Error-proof way of starting SMS intent in Android - 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...)

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

Can i integrate S translator in my app

In my app i want to use galaxy s4 S-translator.So is it possible to use it? And normally can i call another app from my app.I am very new to android.So I don't know it is possible or not.So if possible plz tell me.I think by using intent with giving proper action we can do it.
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("app package name");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
thanx
Yes, by using intents.
For example:
final Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.setClassName("com.example.theotherapp", "com.example.theotherapp.MainActivity");
startActivity(intent);
This is called an explicit intent, because you're explicitly stating which component should respond to it. You can also use implicit intents, in which you specify what kind of component you expect and the OS and/or the user selects the most appropriate one.
If you can choose, implicit intents are preferred.
This might Help You.

Android - Opening Default Music Library Properly

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)
}

Starting an external activity, getting permission error

I'm trying to start the preferences activity in the native messenger client from my application. in AOSP Mms.apk does not have an intent filter setup on that activity. Regardless I'm trying to find a work around to launch the user into that screen.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(ComponentName.unflattenFromString("com.android.mms/com.android.mms.ui.MessagingPreferenceActivity"));
intent.addCategory("android.intent.category.LAUNCHER");
try {
startActivity(intent);
} catch (Exception e) {
AppUtils.alertError(this, error);
}
I'm receiving
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.mms/.ui.MessagingPreferenceActivity } from ProcessRecord{406e2738 674:com.handmark.genericapp/10034} (pid=674, uid=10034) requires null
Any thoughts?
What you want is not possible. That activity is not exported (at least in the source code showing in Google Code Search), so you cannot start it, except by rewriting the app as part of your own custom firmware.
Also, bear in mind that this app may or may not exist on any given device.

Launching Android Weather App Via Intent

I want to launch the stock weather app from my android app via an
intent, but I keep getting a force close runtime error, and LogCat
gives me nothing. The code I am trying to use to achieve this is:
public void startWeatherActivity() {
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(ComponentName.unflattenFromString("org.anddev.android.weatherforecast/
org.anddev.android.weatherforecast.WeatherForecast"));
intent.addCategory("android.intent.category.LAUNCHER");
startActivity(intent);
}
Try to get your intent from getLaunchIntentFromPackage:
android doc
just as a comment, you should use Intent.ACTION_MAIN instead of the explicit string constant.

Categories

Resources