Launching Android Weather App Via Intent - android

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.

Related

Open About Phone using Intent Android

I just want to open About Phone using intent to get the Bluetooth MAC address, I know how to open intent but I don't know the required action. Below are some of the common intents but I didn't find the one I need to open about the phone.
Commen Intents
Following is the code to open intent Action
Intent intent = new Intent(Settings.ACTION_....);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
oh it is simple
use ACTION_DEVICE_INFO_SETTINGS to open device info
Intent intent = new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS);
startActivity(intent);

Launch an activity of an application from a different application on Android

I need to launch an activity (not the main activity) of an application from an application I have made. The activity I want to launch is proprietary, hence, I cannot make any changes to its code(or manifest).
For example: I want to launch somebody's Facebook profile from my own application. A normal intent to facebook from my app would open the 'newsfeed'(which I don't want). I want to know how to access any other activity.
Thanks in advance!
The little code I have:
String PACKAGE="com.facebook.katana";
Intent launchIntent = getPackageManager()
.getLaunchIntentForPackage(PACKAGE);
startActivity(launchIntent);
To launch specific activity you need to use explicit intent. Or use implicit intent with action if you know what action that activity answers to.
To use explicit intent you can do the following (provided you call it from the activity):
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.package.name", "com.package.name.ActivityName"));
if(getPackageManager().resolveActivity(intent, 0) != null) {
startActivity(intent);
} else {
Toast.makeText(this, "No app installed that can perform this action", Toast.LENGTH_SHORT).show();
}
You can also add flags to the intent, add actions and categories. As long as the intent can be resolved as viable intent by the PackageManager, it will launch the activity.
Now...
The question about facebook profile, is a different one.
Perhaps, the best way to achieve that would be to use intent with action VIEW and povide Intent.setData with uri to the profile page. That should also be checked for possibility of being resolved correctly. And then will launch the chooser of all supported activities to open it, which should include facebook application. It is then up to user to open the intent using Facebook app or launcher.

How (i.e., what intent action) to start the set up email account activity (add new email account activity) of the email application

From within my app, I'd like to start the set up new email account activity of the Email App which looks like this: http://i.stack.imgur.com/BNYnj.png
I've looked at this http://source-android.frandroid.com/packages/apps/Email/AndroidManifest.xml
and tried to start the set up email activity:
Intent intent = new Intent("com.android.email.CREATE_ACCOUNT");
startActivity(intent);
But I got an exception:
E/AndroidRuntime(517): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.email.CREATE_ACCOUNT }
Anyone please help me?
Thanks so much,
John
you could try using an explicit intent. instead of
new Intent("com.android.email.CREATE_ACCOUNT")
use
new Intent(context, com.android.email.activity.setup.AccountSetupBasics.class)
you may also want to look into the whole ACTION_ADD_ACCOUNT action string. it may do what you are looking for without having to use a SPECIFIC app. for example, when an oem installs a different email app from the stock android one. if it happens there won't be anything to handle either the explicity or implicit intent.
This works for from APIs 4.0+.
Intent intent = new Intent("com.android.email.CREATE_ACCOUNT");
intent.putExtra("FLOW_MODE", 0);
startActivity(intent);
Below works for from APIs 2.1+. Maybe also work for lower versions (not tested).
Intent intent = new Intent();
intent.setClassName("com.android.email", "com.android.email.activity.setup.AccountSetupBasics");
intent.putExtra("FLOW_MODE", 0);
startActivity(intent);

Launch CallLog activity in android using Intent

I'm able to launch the Call Log activity using intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(android.provider.CallLog.Calls.CONTENT_URI);
startActivity(intent);
But on selecting any Log (incoming, Outgoing or missed ) from Call Log activity it will initiate a call.
But according to my requirement i want to use above intent using action Intent.ACTION_PICK and startActivityForResult(intent).
please share any idea to do the same.

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