Search code examples
androidadb

Android call OEM activity by component name


On a Huwaie Ascend, when we walk through the settings menu:

Settings -> SD card & phone storage -> Software Upgrade -> SD card Upgrade

We are then brought to a screen where the user can upgrade.

And then using adb logcat we see this:

 Starting activity: Intent { act=android.intent.action.MAIN cmp=com.android.settings/.SystemUpgradeCheck }

We can use adb to simulate this by calling:

adb shell am start -n com.android.settings/.SystemUpgradeCheck

This is successful, and we see the screen.

However, when we try to call this from within an activity like this:

    Intent i = new Intent(Intent.ACTION_MAIN);
    i.setComponent(new ComponentName("com.android.settings", ".SystemUpgradeCheck"));
    startActivity(i);

We get this error:

Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.settings/.SystemUpgradeCheck}; have you declared this activity in your AndroidManifest.xml?

What can we do to overcome this? Am I calling the intent wrong?


Solution

  • Figured it out :)

    Context foreignContext = createPackageContext("com.android.settings", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
    Class<?> yourClass = foreignContext.getClassLoader().loadClass("com.android.settings.SystemUpgradeCheck");
    
    Intent intent = new Intent(foreignContext, yourClass);
    startActivity(intent);