通过包名称启动活动



我一直在测试一些我没有源代码的软件包,其中一个软件包通常通过按三个按钮三秒钟来启动。当我尝试使用典型方法启动包时,出现java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.addFlags(int)错误。下面是我的代码

@Before
public void setup() {
//Initialize UiDevice instance
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
mDevice = UiDevice.getInstance(instrumentation);
mDevice.pressHome();
final String launcherPackage = mDevice.getLauncherPackageName();
assertThat(launcherPackage, notNullValue());
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
Context context = InstrumentationRegistry.getTargetContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(DEALER_DIAG_PACKAGE);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
mDevice.wait(Until.hasObject(By.pkg(DEALER_DIAG_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}

我尝试使用getContext而不是getTargetContext,但是有人向我指出,如果不导出意图,无论我做什么,我都无法以这种方式启动包。我尝试使用命令获取包名称adb logcat ActivityManager:V *:F以及adb shell pm list packages -f

--------- beginning of main
I/ActivityManager( 2296): START u0 {flg=0x10000000 
cmp=com.android.systemui/.usb.UsbDebuggingActivity (has extras)} from uid 
1000 on display 0
I/ActivityManager( 2296): Displayed 
com.android.systemui/.usb.UsbDebuggingActivity: +184ms
I/ActivityManager( 2296): START u0 {act=android.intent.action.MAIN cat=
[android.intent.category.HOME] flg=0x10200000 
cmp=com.android.launcher3/.Launcher} from uid 1000 on display 0
I/ActivityManager( 2296): START u0 
{act=com.REDACTED.auto.diagnostics.dealer.MAIN flg=0x10800000 
cmp=com.REDACTED.auto.diagnostics/.dealer.MainActivity} from uid 1000 on 
display 0
I/ActivityManager( 2296): Start proc 
20943:com.REDACTED.auto.diagnostics/1000 for activity 
com.REDACTED.auto.diagnostics/.dealer.MainActivity
I/ActivityManager( 2296): Displayed 
com.REDACTED.auto.diagnostics/.dealer.MainActivity: +572ms

有没有人对我为什么收到此错误有任何意见?我尝试使用logcat转储中列出的每个包名称,但没有成功。任何意见将不胜感激。

如果您想启动另一个应用程序,只需先检查此应用程序是否已安装,否则您将获得NullPointerException,然后通过软件包选项启动该应用程序:

就像这样:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.domain.anotherapp");
if (launchIntent != null) {
startActivity(launchIntent);//null pointer check in case package name was not found
}

这将使用默认启动活动启动应用程序,如果您想启动特定活动,您已经必须知道如何处理其他应用程序端的要求,或者只是崩溃或不起作用(可能需要传递一些变量或一些数据来表示此应用程序的先前信息), 无论如何,要打开此特定活动,您必须使用ComponentName.

采用两个StringComponentName构造函数可用于引用另一个应用程序中的组件。 但是,第一个参数不是类的包名;它是应用程序的包名称---该应用程序AndroidManifest.xmlmanifest元素的package属性。 所以你的第一个例子应该是

ComponentName cn = new ComponentName("com.domain.anotherapp",
"com.domain.anotherapp.widget.WidgetProvider");

该构造函数当然可用于引用您自己的应用程序中的组件,但由于您已经掌握了自己应用程序中的Context,因此您不妨使用它并使用其他构造函数之一。 在我看来,只要可用,就应该首选Class。 如果您仅出于某种原因动态了解该类,则可以使用String的那个;在这种情况下,它应采用上述完全限定的类名。

通过意图启动的完整用途(不处理不正确传递信息的空指针异常):

Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName("com.domain.anotherapp",
"com.domain.anotherapp.widget.WidgetProvider");
launchIntent.setComponent(cn);
startActivity(launchIntent);

参考: https://developer.android.com/reference/android/content/ComponentName.html

我想出了一个适合我的方法,尽管布兰登的方法可能适用于另一种解决方案。这是我的解决方案:

Intent intent = new Intent("com.REDACTED.auto.diagnostics.dealer.MAIN");
intent.setClassName("com.REDACTED.auto.diagnostics", 
"com.REDACTED.auto.diagnostics.dealer.MainActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Context c = InstrumentationRegistry.getContext();
c.startActivity(intent);

这可能与布兰登的解决方案做同样的事情,但不太抽象。

最新更新