Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Intent to start Main activity of application

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?

like image 704
Abhishek Avatar asked Jun 14 '12 20:06

Abhishek


People also ask

How do I launch an activity using intent?

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.

Where do you define the starting activity of your application in Android?

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.

What is Android intent action Main?

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.


2 Answers

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);
}
like image 100
TienLuong Avatar answered Oct 18 '22 12:10

TienLuong


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);
like image 29
Tal Kanel Avatar answered Oct 18 '22 13:10

Tal Kanel