Sunday, June 17, 2012

Android- Link to Google Store for Ranking


User reviews of Android Apps and rankings are the key metric by which Android apps are measured. I've added buttons to many of my apps to make it easy for users to create a review for my app from the app.  All you need is the url to the app and the code below.

// the package name as registered in the manifest.
public static final String URL_LINK_DEST = "http://market.android.com/search?q=pname:com.yourapp.appname.android";


 Button btnRate= (Button) findViewById(R.id.btnRateThisApp);
       btnRate.setVisibility(View.VISIBLE);
       btnRate.setClickable(false);
       btnRate.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(GlobalConstants.URL_LINK_DEST));
    startActivity(i);  
}

});

Wednesday, June 13, 2012

Sending an SMS


Sending an SMS from your app is actually quite simple.  Note that once you add sms capabilities to your app, devices (like tablets) that don't have SMS can't install the app.



 String message= "Watson come here"; 

 String watsonsCell= "1234567890"; 

 SmsManager sms = SmsManager.getDefault();  

  if (message!=null) { 

   sms.sendTextMessage(watsonsCell, null, message, null, null); 

 }  



You need to add a permission to the AndroidManifest.



 <uses-permission android:name="android.permission.SEND_SMS"/>  



If you want to process an sms, you need the following permission, but it isn't necessary for sending.



 <uses-permission android:name="android.permission.RECEIVE_SMS"/>  


Sunday, June 10, 2012

Android Bug Report Database

A very useful resource is the official Android bug report database located at http://source.android.com/source/report-bugs.  This page allows developers to report bugs and search for known issues.  This is particularly useful for debugging problems reported for specific devices or for different API levels.  For example, I've used this to determine problems with JSON processing failure on a particular HTC device or problems debugging camera integration on Samsung tables and  differences between GMAIL support on api levels.  So check it out and book mark the page. Sooner or later it will come in handy!

Saturday, June 9, 2012

Starting Services on Boot


Most of the projects I work on have lots of backend integrations,some of which update data on a regular basis.  So I set up these integrations within a service that gets started when the device is turned on.  Pretty easy to do. I set up an array in the arrays.xml that has the full path to the service I want to start (packagename+servicename") so for example com.me.myproject.android.services.mygreatservice

The boot service gets the Broadcast message that Android sends out on startup, iterates through array of services and starts them one by one.

You'll need to add the permission below and register the receiver class in the manifest.



  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <receiver android:name=".services.BootReceiver" >
       <intent-filter >
         <action android:name="android.intent.action.BOOT_COMPLETED" />
       </intent-filter>
     </receiver>      



 public class BootReceiver extends BroadcastReceiver { 
      private static final String TAG ="BootReceiver";
      /**
       * Service intent
       */
      public static Intent mIntent;
      @Override
      public void onReceive(Context context, Intent intent) {
           String[] servs= context.getResources().getStringArray(R.array.services);
            for (int i=0; i<servs.length; i++) {
                 Log.d(TAG, "Starting: "+servs[i]);
                     Intent mIntent = new Intent();                                
                     mIntent.setAction(servs[i]);
                     context.startService(mIntent);
            }
      }
 }  

Wednesday, June 6, 2012

Illegal Access Error and Referenced Projects

I've been writing a number of test classes for a very complex Android app. Recently I ran into a problem with a third party jar file misbehaving and throwing a java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation.  This is due to a class path problem. The solution is to reference like you normally would the jar in the source app and BE SURE TO EXPORT IT.(last tab in Eclipse properties-->Classpath


Then in the test app you MUST REFERENCE the project. Thanks to this blog for the excellent details on what to do!

http://blog.js-development.com/2010/06/android-instrumentation-test.html

Sunday, June 3, 2012

Deleting files from the SDCard

As part of a recent project, I enabled users to export data from the database to the file system by clicking a button.  Users then wanted an easy way to delete the files that they created.   This little bit of code below does just that. It looks in the standard Android download directory (where I'm saving the files) for all the files that end in testfile.txt (I'm naming files {_id}_{date}_testfile.txt) and then deletes them one by one..



 Button clearJsonFiles = (Button) findViewById(R.id.clearJsonFiles); 
           clearJsonFiles.setOnClickListener(new OnClickListener()
           {
                public void onClick(View v)
                {
                     File path = Environment
                         .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                     File[] files = path.listFiles();
                     for (int i = 0; i < files.length; i++)
                     {
                          try {
                               Log.d(TAG, "Found File:" + files[i].getCanonicalFile());
                               if (files[i].getCanonicalPath().contains("testfile.txt")) {
                                    Log.d(TAG, "Deleting File:" + files[i].getCanonicalFile());
                                    //deleteExternalStoragePublicFile(files[i].getCanonicalPath());
                                    files[i].delete();
                               }
                          } catch (IOException e) {
                               // TODO Auto-generated catch block
                               e.printStackTrace();
                          }
                     }
                }
           });  

Tuesday, May 29, 2012

Android Coding Standards

One of the challenges facing any large development effort is ensuring that all developers follow the same coding standards. Maintaining coding standards, which is "a set or rules or guidelines for formatting code", has numerous benefits including helping lower the cost of code maintenance, enabling new developers to learn the code base faster and overall lower cost of ownership. This link talks about the benefits of coding standards in a very concise and easy to understand way.

http://www.valid-computing.com/benefits-of-coding-standards.html

The Android development team has its own code standards that they use for the Android source. It's published here.  http://source.android.com/source/code-style.html  I try to follow these standards the best that I can, although individual projects may have their own set of standards that should be followed.  The important thing is to have standards and to make sure that everyone uses them.