Android - Opening Default Music Library Properly - android

In my Android application I have a button which should open the Music Library of device.
I'm using the following code now
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(intent);
I understand that there can be devices without default music library.
So, I want the code snippet which will open music library if it is possible and show some message(or something else), otherwise.

Surround it in a try/catch block. If an ActivityNotFoundException is thrown, then there is no component on the device capable of receiving that intent:
try {
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(intent);
catch (ActivityNotFoundException anfe) {
// Handle no component found here (e.g. show a toast or dialog)
}

Related

Is there a way to open a browser tab automatically in Android?

I'm looking for a way to open a browser at a specific link automatically at a give time / daily.
Maybe there is some kind of script, add-on for bowser but I still haven't find one.
Does anyone know a good solution?
I suggest you to use an AalarmManager here is an example
And then here is the code you should execute
String urlString = "your_url";
Intent intent = new
Intent(Intent.ACTION_VIEW,Uri.parse(urlString));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.chrome");
try {
context.startActivity(intent);
}
catch (ActivityNotFoundException ex) {
context.startActivity(intent);
}
Seems to me this is a combination of three things, setting the alarm, firing the intent and making sure the intent has the data to open a specific uri in the browser.
start activity from an alarm
open a uri

Running market intent exits my main application

My application needs QR code scanner app for running properly. There is no problem to request it like:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData( Uri.parse( "market://details?id=something" ));
startActivity(intent);
After this I am redirected to Google market with predefined application. However my problem is that this code exits my main application. It is not in list. Is this a correct behaviour? When you want to install some other application does it exit other applications? Or am I doing something wrong?
Main goal is to just bring to front my main application to use it after QR code scanner is installed.
Any help is appreciated.
Have you write finish() in your previous activity from where you are redirecting to google play activity. If you have written then remove it.
final String APPLICATION_PACKAGE_ID = "com.google.zxing.client.android";
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APPLICATION_PACKAGE_ID)));
} catch(ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + APPLICATION_PACKAGE_ID)));
}
So I just make like it is. And when the application is installed I just hit back and it works. I dont understand how didn't work this in friday but now it is ok.

How to prevent "complete action using" while opening pdf file using Adobe Reader.

I want to open pdf file using installed adobe reader. I tried in the following way to prevent "Complete action using" menu.
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.fromFile(doc), "application/pdf");
startActivity(intent);
Using the above code i managed to reduce the list size to 2. Is there any way to avoid showing context menu (Complete action using).
Thank you!
Android system automatically displays "complete action using" dialog box when two activities have same intent filter action. Once you select the default action. Android will not display it & complete the task using default action.
I would like to thanks every one who tried to answer this question. After some browsing i found solution for my Question. which is perfectly working for me.
instead of using
intent.setPackage("com.adobe.reader");
I used
intent.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader");
don't forget to start activity in try catch block, it helps when adobe reader not installed on Device. Please check the below snippet.
try {
Intent intent = new Intent();
intent.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader");
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(doc), "application/pdf");
startActivity(intent);
}
catch (ActivityNotFoundException activityNotFoundException) {
activityNotFoundException.printStackTrace();
}

Opening my application from Google Play App crashes on exit

I have option to open my app Google Play page inside my app. When clicking open button in Google Play My app launches again. (From Splash screen)
When I am exiting my app it crashed. I tried put singleTask flag to my Home activity. It actually worked fine. But arised another crash. So I need to know is:
Is there any option to put flag in my Market calling Intent to notify that the app is already launched and just bring it front on clicking Open button?
Here how am I calling GooglePlay app.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(marketUrl));
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Cannot find Android Market",
Toast.LENGTH_LONG).show();
}
EDIT
My market url: market://details?id=com.foo.bar
This will directly redirect to my apps Installation page.
use this code..
Uri uri = Uri.parse(https://play.google.com/store);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

Error-proof way of starting SMS intent in Android

In my Android application, I use the following code to start the messaging app and fill in a default text for a text message:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("sms:"+USERS_PHONE_NUMBER));
intent.putExtra("sms_body", "DUMMY TEXT");
startActivity(intent);
This works in most cases. But unfortunately, on some devices I get the following error message:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=sms:+XXXXXXXXXX (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1510)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
at android.app.Activity.startActivityForResult(Activity.java:3131)
at android.app.Activity.startActivity(Activity.java:3237)
Obviously, the intent that I created cannot be handled.
Is there any mistake in my SMS intent code?
How can I prevent the application from crashing if the intent cannot be handled?
Should I use PackageManager.queryIntentActivities() or is there another way of solving this problem?
Thanks in advance!
I haven't tried this intent specifically, but the simplest way will probably be to add try and catch block
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Display some sort of error message here.
}
Since you can't count on the specific Android device to have the Messaging app (some tablets for example don't have telephony services), you have to be ready.
It is a good practice in general when you're starting external activities, to avoid crashes in your app.
Here is the code that will open the SMS activity pre-populated with the phone number to
which the SMS has to be sent. This works fine on emulator as well as the device.
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setData(Uri.parse("sms:" + phoneNumber);
Here is a method I use to safely open activities on Android, and give the user some feedback if the activity is not found.
public static void safeOpenActivityIntent(Context context, Intent activityIntent) {
// Verify that the intent will resolve to an activity
if (activityIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(activityIntent);
} else {
Toast.makeText(context, "app not available", Toast.LENGTH_LONG).show();
}
}
(I think I got it from one of the Google Developers videos on youtube, but now I can't find the video...)

Categories

Resources