3

In my application I need to start the main activity the following way as documented in several other threads on SO:

final String packageName = getApplicationContext().getPackageName();
final Intent launchIntent = getPackageManager().getLaunchIntentForPackage(packageName);
startActivity(launchIntent);

The above line of code is in my Android library project as I don't know at this place the class of my Main activity.

This works like a charm when deploying app trough the USB interface on test devices but this error occurs when installing app from Google Play on 4.3 devices.

07-03 13:54:19.843: E/DatabaseUtils(2344): Writing exception to parcel 07-03 13:54:19.843: E/DatabaseUtils(2344): java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL 07-03 13:54:19.843: E/DatabaseUtils(2344): at com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:13140) 07-03 13:54:19.843: E/DatabaseUtils(2344): at android.app.ActivityManager.handleIncomingUser(ActivityManager.java:2038) 07-03 13:54:19.843: E/DatabaseUtils(2344): at com.android.providers.settings.SettingsProvider.callFromPackage(SettingsProvider.java:607) 07-03 13:54:19.843: E/DatabaseUtils(2344): at android.content.ContentProvider$Transport.call(ContentProvider.java:279) 07-03 13:54:19.843: E/DatabaseUtils(2344): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:273) 07-03 13:54:19.843: E/DatabaseUtils(2344): at android.os.Binder.execTransact(Binder.java:388) 07-03 13:54:19.843: E/DatabaseUtils(2344): at com.android.server.SystemServer.init1(Native Method) 07-03 13:54:19.843: E/DatabaseUtils(2344): at com.android.server.SystemServer.main(SystemServer.java:2012) 07-03 13:54:19.843: E/DatabaseUtils(2344): at java.lang.reflect.Method.invokeNative(Native Method) 07-03 13:54:19.843: E/DatabaseUtils(2344): at java.lang.reflect.Method.invoke(Method.java:525) 07-03 13:54:19.843: E/DatabaseUtils(2344): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 07-03 13:54:19.843: E/DatabaseUtils(2344): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 07-03 13:54:19.843: E/DatabaseUtils(2344): at dalvik.system.NativeStart.main(Native Method)

Has someone experienced the same problem?

Edit:
I didn't mentioned that several threads in SO mentioned that "android.permission.INTERACT_ACROSS_USERS_FULL is a signature level permission. Your app will not be able to use it until and unless it has the same signature as the system."

Permission Denial: startActivity asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
Permission Denial: this requires android.permission.INTERACT_ACROSS_USERS_FULL What do I do about: this requires android.permission.INTERACT_ACROSS_USERS_FULL

But as the above code seems to be used by other developers, I'm still hoping for a solution.

Edit 2:
Remove stack not related to problem.

Community
  • 1
  • 1
L. G.
  • 9,642
  • 7
  • 56
  • 78

3 Answers3

0

Quoting from what-do-i-do-about-this-requires-android-permission-interact-across-users-full,

android.permission.INTERACT_ACROSS_USERS_FULL is a signature level permission. Your app will not be able to use it until and unless it has the same signature as the system.

UPDATE:

Do you have the android.permission.WRITE_EXTERNAL_STORAGE ?

This link may help you - How permission can be checked at runtime without throwing SecurityException?

Your concern -

PackageManager pm = context.getPackageManager();
int hasPerm = pm.checkPermission(
    android.Manifest.permission.WRITE_EXTERNAL_STORAGE, 
    context.getPackageName());
if (hasPerm != PackageManager.PERMISSION_GRANTED) {
   // do stuff
}
Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • Yes, I already read this and should have mentioned it in my question. But as I see other programmers using the above code this should be possible... Unless a restriction appeared at one Android API level. – L. G. Jul 03 '14 at 13:01
  • That's perhaps a good hint, one line of code above I access external storage but my app has WRITE_EXTERNAL_STORAGE permission. I will improve my error handling and search this way, thanks. – L. G. Jul 03 '14 at 13:22
0

Unfortunately the system didn't:

  • throw any exception
  • log the cause of the error

The exception in my question was not related to the problem and was not logged on other devices.

We used following workaround that works on all devices for now to solve the problem and launch the main activity from an Android library:

protected void startMainActivityWithWorkaround() throws NameNotFoundException, ActivityNotFoundException {
    final String packageName = getPackageName();
    final Intent launchIntent = getPackageManager().getLaunchIntentForPackage(packageName);
    if (launchIntent == null) {
      Log.e(LOG_TAG, "Launch intent is null");
    } else {
      final String mainActivity = launchIntent.getComponent().getClassName();
      Log.d(LOG_TAG, String.format("Open activity with package name %s / class name %s", packageName, mainActivity));
      final Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_LAUNCHER);
      intent.setComponent(new ComponentName(packageName, mainActivity));
      // optional: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(intent);
    }
}
L. G.
  • 9,642
  • 7
  • 56
  • 78
  • Hello there!I get the same exact error as you used to get and it's as frustrating as it can freaking get! Although,I get the error when I try to make an UDP connection and send a message to the server...any chance you would be able to give me some hints on how to solve it? – Vlad Jul 04 '15 at 11:47
  • As explained above the SecurityException was not related to my problem to launch the main activity. The workaround above solve the problem of getting a null launch intent with the package manager by recreating an intent using the main activity class name. – L. G. Jul 06 '15 at 07:52
0

Check to make sure you didn't forget to add an activity to your manifest.

Droid Teahouse
  • 893
  • 8
  • 15
  • This was not the problem, when you forget to add an activity in your manifest you get an explicit exception ActivityNotFoundException. – L. G. Jul 06 '15 at 07:55