如何从其他应用启动Android Activity?

mutmk8jj  于 2023-01-07  发布在  Android
关注(0)|答案(4)|浏览(200)

我正在尝试编写一个启动Android STK活动的应用程序,如下所示:

Intent intent = new Intent(); 
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            intent.addCategory(Intent.CATEGORY_LAUNCHER); 
            intent.setAction(Intent.ACTION_MAIN); 
            intent.setComponent(new ComponentName("com.android.stk", "com.android.stk.StkLauncherActivity")); 
            startActivity(intent);

我一直收到以下错误:

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

我在旅客名单上声明了以下内容:

<activity android:name="com.android.stk.StkLauncherActivity"/>
vxf3dgd4

vxf3dgd41#

尝试使用PackageManager.getLaunchIntentForPackage,它返回一个在给定包中启动前门Activity的Intent:

PackageManager manager = getPackageManager(); 
   Intent intent =manager.getLaunchIntentForPackage("com.android.stk"); 
   if (intent != null)  
    startActivity(intent);
acruukt9

acruukt92#

看起来你这边有个打字错误

com.android.stk/com.android.stk2.StkLauncherActivity

stk还是stk2?:)

lkaoscv7

lkaoscv73#

要从其他应用启动Activity,您可以在Activity所属应用的Android清单Activity Intent过滤器中设置“action”。启动Activity时,将相同的“action“设置为Intent

xienkqul

xienkqul4#

尝试以下代码片段

final Intent intent = new Intent(Intent.ACTION_MAIN, null);

intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.abc.xyz", "com.abc.xyz.MainActivity");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( intent);

相关问题