Calling system applications in Android


In my previous posts I have used reading contacts through API calls, however there is an easy method for doing standard tasks by calling system’s pre installed apps. In this post I am going to cover the details of how we can call the system apps through intents.

We are going to create a spinner control for providing the user some options and hit a submit button.

Here’s main.xml which defines the UI.

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
     >  
   <Spinner  
       android:id="@+id/spinner1"  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:entries="@array/application_array"  
       android:prompt="@string/app_prompt" />  
   <Button  
       android:id="@+id/btnSubmit"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="Submit"  
       android:onClick="btnSubmitOnClick"/>  
 </LinearLayout>  

Spinner control requires a string array (application_array) for it’s entries which is defined in strings.xml.

 <?xml version="1.0" encoding="utf-8"?>  
 <resources>  
   <string name="app_name">callingsystemapps</string>  
   <string name="app_prompt">Choose a application</string>  
   <string-array name="application_array">  
     <item>Browser</item>  
     <item>Dialler</item>  
     <item>Map</item>  
     <item>Contacts</item>  
   </string-array>  
 </resources>  

In main.xml we have also have defined onClick method btnSubmitOnClick which is defined in MyActivity.java

   public void btnSubmitOnClick (View v)  
   {  
     Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);  
     if (spinner1.getSelectedItem().toString().equals("Browser")){  
       Intent i = new  
           Intent(android.content.Intent.ACTION_VIEW,  
           Uri.parse("http://www.google.com"));  
       startActivity(i);  
     }  
     else if (spinner1.getSelectedItem().toString().equals("Dialler")){  
       Intent i = new  
           Intent(android.content.Intent.ACTION_DIAL);  
       startActivity(i);  
     }  
     else if (spinner1.getSelectedItem().toString().equals("Map")) {  
         Intent i = new  
             Intent(android.content.Intent.ACTION_VIEW,  
             Uri.parse("geo:37.827500,-122.481670"));  
         startActivity(i);  
     }  
     else if (spinner1.getSelectedItem().toString().equals("Contacts")) {  
       Intent i = new  
           Intent(android.content.Intent.ACTION_PICK);  
       i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);  
       startActivity(i);  
     }  

Only difference between the four is the passing intent which is as below:

ACTION_VIEW
ACTION_DIAL
ACTION_PICK

Along with that we are passing parameters depending upon intent type. After that it’s just calling startActivity on the intent.

Try running the example and you shall get like this.

getActionBar and targetSdkVersion in Android development


Hi All,Recently I was trying to reiterate Android docs training just to fill in anything I missed previously. There I came across this piece of code:

// Initialize member TextView so we can manipulate it later  
     mTextView = (TextView) findViewById(R.id.edit_message);  
     // Make sure we're running on Honeycomb or higher to use ActionBar APIs  
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {  
       // For the main activity, make sure the app icon in the action bar  
       // does not behave as a button  
       ActionBar actionBar = getActionBar();  
       actionBar.setHomeButtonEnabled(false);  
     }

I tried to put that in a simple hello world example and it worked on an 2.2 emulator, however it failed to run on 4.0 emulator. On debugging it turned out that getActionBar was returning null value. Though there was on-line help available for some other directions it just came to my mind to check AndroidManifest.xml file. There I saw only “android:minSdkVersion=”8″” I remembered that there was a target SDK  as well which can be defined. So I checked a project created by eclipse and found the “android:targetSdkVersion=”15″” line and added it to “uses-sdk ” tag in AndroidManifest.xml and the app started working on 4.0 emulator.

This clears two things, first is that Idea IDE adds only minSDKVersion while eclipse adds targetSDKVersion as well. Which is reflected in new project also but could effect like this.

Second, if you search for getActionBar returning null you get loads of advice but maybe not this. So check if you are having this issue.

Thanks for reading. Thanks for Android guys for creating such a good documentation, which by and large so much better than Facebook developer docs. Only wish Facebook docs were also as clear as the Android docs.

Nexus 4 unboxing and first impressions


Google’s newest flagship handset, the LG Nexus, 4 is finally upon us. The highly anticipated stock Android smartphone features some of the highest-end specs of any phone currently on the market, but does it manage to satisfy? Stay tuned for a complete review. However, in the meantime, be sure to catch our unboxing and first impressions below.

http://supertechblog.com/2012/11/17/nexus-4-unboxing-and-first-impressions/

via Blogger http://ashish-yadav.blogspot.com/2012/11/nexus-4-unboxing-and-first-impressions.html