Saturday, March 28, 2015

Android Launching System Apps

As part of my experiments, i had to launch two system apps Music Player and Map. Some notes from experience wrote down here. 

Below are few ways to launch native Android default applications from a Google play available application


Launching Music Player

 Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

This was working perfectly, however, in the code it was saying MediaStore.INTENT_ACTION_MUSIC_PLAYER is deprecated. 

The replacement for this was CATEGORY_APP_MUSIC 

As per the documentation, this Category should be used along with the ACTION_MAIN to launch music application. The activity should be able to play, browse, or manipulate music files stored on the device. 

This should not be used as a primary key for the intent since it will not result in the app launching with correct action and category. instead, we should use this with makeMainSelectorActivity(String, String) to generate a main intent with this category in the selector. 

However, below code was resulting in No Activity Found 

 Intent intent = new Intent();
                        intent.makeMainSelectorActivity(intent.ACTION_MAIN,
                                Intent.CATEGORY_APP_MUSIC);
                        startActivity(intent);

For time being decided to stay with the old approach itself. 

Launching google Map Application 

 String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=%f,%f (%s)", 12.914081f, 77.609953f, "Cafe Coffee day");
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                        intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
                        try
                        {
                            startActivity(intent);
                        }
                        catch(ActivityNotFoundException ex)
                        {
                            try
                            {
                                Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                                startActivity(unrestrictedIntent);
                            }
                            catch(ActivityNotFoundException innerEx)
                            {
                                //Toast.makeText(this, "Please install a maps application", Toast.LENGTH_LONG).show();
                            }
                        }

The above code launches the map and shows the navigation view. 

Uri gmmIntentUri = Uri.parse("geo:?z=15");
                        Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                        mapIntent.setPackage("com.google.android.apps.maps");
                        startActivity(mapIntent);

The above code just launches the map view with zoom level as 15 and focuses the map with the current location of the user. 

References:

No comments:

Post a Comment