I am trying to start the main activity from inside a BroadcastReceiver. I dont want to supply the activity class name but to use the action and category for android to figure out the main activity.
It doesnt seem to work.
Sending Code:
Intent startIntent = new Intent();
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startIntent.setAction(Intent.ACTION_MAIN);
startIntent.setPackage(context.getPackageName());
startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(startIntent);
I get this error:
Caused bt: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.xyz.abc (has extras) }
Any ideas?
1.2. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.
In Android, you can configure the starting activity (default activity) of your application via following “intent-filter” in “AndroidManifest. xml“. See following code snippet to configure a activity class “logoActivity” as the default activity.
android. intent. action. MAIN means that this activity is the entry point of the application, i.e. when you launch the application, this activity is created.
Copy from another topic:
This works since API Level 3 (Android 1.5):
private void startMainActivity(Context context) throws NameNotFoundException {
PackageManager pm = context.getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(context.getPackageName());
context.startActivity(intent);
}
this is not the right way to startActivity.
try this code instead:
Intent startIntent = new Intent(context, MainActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With