Monday, August 6, 2012

Determining where your Android APK is installed

A little bit of code can tell you where your apk files are being installed. Typically it will be /data/app/package.name.apk



07-25 09:15:59.065: D/Main(3000): Source directory at: /data/app/com.appulearn.supersecretapp.android-1.apk

This bit of code will tell you....


  PackageManager  pmMgr = getPackageManager();
        List pkgList = pmMgr.getInstalledPackages(PackageManager.GET_ACTIVITIES);
        List appinfo_list = pmMgr.getInstalledApplications(0);
        for (int x=0; x < pkgList.size(); x++){    
          Log.d(TAG, "Source directory at: " +appinfo_list.get(x).sourceDir);
        }

Friday, August 3, 2012

Designing Android Databases

Most of my app use a sqlite database and I use data scripts to create, alter and bootstrap tables. ADB comes with the ability to look at the database on a USB connected device, but I often test the scripts in an opensource tool called SqliteBrowser.  Unfortunately it's a little bit unstable, and seems like development on it has stopped but it's great for testing sql statements that you want to run within an Android app/

http://sqlitebrowser.sourceforge.net/

Tuesday, July 31, 2012

Android: Disabling or Blocking the back button

One of the tablet apps I've been working on is a massive data collection app. It is set up to run in vertical mode and it's easy for the user to accidentally push the back button and exit a data collection form potentially losing their work.  Luckily it is also easy to disable the back button by preventing the click action or putting a dialog up when it is touched to confirm the exit of the screen.

Here you go. Nothing to it.

@Override
public void onBackPressed() {
}

Thursday, July 26, 2012

Android logcat disappears with multiple devices attached

I was recently working on an Android project testing on multiple devices attached via USB and rotating the testing between them. After a while, all trace stopped being displayed and wouldn't come back. I disconnected devices, reset the debugging perspective, restarted Eclipse, restarted the laptop but nothing worked.  I typically have more than one Android device plugged in and in the past two years have never had any problems.  The solution was to go into DBMS perspective and select the specific device in the upper left hand panel. For whatever reason, it wasn't automatically switching based on what was running.

Monday, July 23, 2012

Data type, Android and Sqllite



When working with Android content providers and cursors, pay close attention to the datatypes you are using to pull the data from the database. If you set the data type of the sqlite field to NUMERIC and are using it to store LONG values, be sure to pull it from the database as a long and not a int. Pulling it as an Integer will lead to truncation of the long value. So for example,
Since CreateDate is a long, I need to pull it like this
data.setCreateDate(c.getLong(c.getColumnIndexOrThrow(DatabaseConstants.F_CREATEDATE)));  

and not like this

data.setCreateDate(c.getInt(c.getColumnIndexOrThrow(DatabaseConstants.F_CREATEDATE)));  

Here's the complete cursor processing.


  while (!c.isAfterLast()) { 

      MyData data = new MyData(); 

      data.setFirstName(c.getString(c.getColumnIndexOrThrow(DatabaseConstants.F_FIRSTNAME))); 

      data.setLastName(c.getString(c.getColumnIndexOrThrow(DatabaseConstants.F_LASTNAME))); 

      data.set_id(c.getInt(c.getColumnIndexOrThrow(DatabaseConstants.F__ID))); 

      data.setCreateDate(c.getLong(c.getColumnIndexOrThrow(DatabaseConstants.F_CREATEDATE))); 

      data.setIsdeleted(c.getInt(c.getColumnIndexOrThrow(DatabaseConstants.F_ISDELETED))); 

      data.setLastUpdate(c.getLong(c.getColumnIndexOrThrow(DatabaseConstants.F_LASTUPDATE))); 

      list.add(data); 

      c.moveToNext(); 

 }  


Thursday, July 19, 2012

More Android code standards

When mapping edit text fields, select boxes and other elements that have GUI interaction with the user  within Android Activity Java code, the first thing you need to know is the resource id where the data will be coming from. Flipping back and forth between the xml resources and the Activities is a pain. Be sure to put a comment in your code next to the variable that indicates the xml resource id like below. This easy piece of documentation and code commenting is invaluable to the developers who will be maintaining your code


 private EditTextView mPersonId;   //   R.id.PersonIdETV
 private Spinner mPersonJob;     //   R.id.PersonJobSPN

Friday, July 13, 2012

Randomly generating a date before or after today



I was recently building a calendar type app for a client and needed to test out the placement of items on the calendar. Unfortunately their test data returned all items with the exact same date. So I wrote a little bit of code that would randomly replace the date in the data with AM PM (the calendar data didn't have a time just whether or not it was in the morning or afternoon and randomly assign a date within a week both before and after today.



 private String generateRandomAmOrPm() {  
         final Random myRandom = new Random();  
         boolean amOrPm=        myRandom.nextBoolean();  
         if (amOrPm) {  
              return "AM";  
         } else {  
              return "PM";  
         }  
      }  
      private int generateRandomDate() {  
            final Random myRandom = new Random();  
              boolean plusOrMinus=        myRandom.nextBoolean();  
              int dateToMove = myRandom.nextInt(6); // plus or minus 6 days  
              if (plusOrMinus) {  
                   return 0-dateToMove;  
              } else {  
                   return dateToMove;  
              }  
      }