Android intention to launch the main action of the application

I am trying to run the main action inside BroadcastReceiver. I do not want to specify the name of the activity class, but use the action and category for android to determine the main activity.

It does not 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 by bt: android.content.ActivityNotFoundException: No activity detected for processing Intent {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] flg = 0x10000000 pkg = com.xyz.abc (has additional functions )}

Any ideas?

+13
android android-intent alarmmanager
source share
4 answers

This is not the right way to startActivity.
try using this code:

 Intent startIntent = new Intent(context, MainActivity.class); startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(startIntent); 
+8
source share

Copy from another topic:

This works with 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); } 
+16
source share

Even I tried to run MainActivity through Activity Activity.

And it worked for me:

 Intent startIntent = new Intent(); startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startIntent.setPackage(getApplicationContext().getPackageName()); getApplicationContext().startActivity(startIntent); 

Make sure you add activity to the library manifest!

+1
source share

Although it’s too late. May be useful for Somone in the future. It helps me.

 Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); this.startActivity(intent); 
0
source share

All Articles