首页 > 解决方案 > 从另一个应用程序启动不是主要活动

问题描述

我有两个申请。假设:appA 和 appB

AppA 具有如下清单:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".EditItemActivity" android:exported="true"></activity>
    <activity android:name=".ListActivity" android:exported="true" />
    <activity android:name=".MainActivity" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

接下来 AppB 创建自定义通知:

public void CustomNotification() {
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.mynotification);

    int idProduct = 1;

    Intent intent_anotherApp = new Intent();
    intent_anotherApp.setComponent(new ComponentName("com.hfade.appA", "com.hfade.appA.ListActivity"));  //does not works

    Intent launchIntent = getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.hfade.appA"); //works but only mainActivity

    Bundle bundle = new Bundle();
    bundle.putInt("pid", idProduct);
    intent_anotherApp.putExtras(bundle);

    PendingIntent pendingIntent = PendingIntent.getActivity(
            this,
            0,
            launchIntent,        // or intent_anotherApp
            PendingIntent.FLAG_UPDATE_CURRENT);

    @SuppressLint({"NewApi", "LocalSuppress"})
    Notification.Builder builder = new Notification.Builder(this, channelID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContent(remoteViews);

    remoteViews.setTextViewText(R.id.txtTitle,"title");
    remoteViews.setTextViewText(R.id.firstLine,"sometext");
    remoteViews.setTextViewText(R.id.secondLine,"text");

    nm.notify(id++, builder.build());
}

但是我有一个问题。当我将此传递给pendingIntent时:

Intent launchIntent = getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.hfade.appA");

它有效,但它只打开一个前门活动(MainActivity)当我尝试使用这种方法时:

Intent intent_anotherApp = new Intent();
    intent_anotherApp.setComponent(new ComponentName("com.hfade.appA", "com.hfade.appA.ListActivity"));

作为挂起活动中的 arg 不起作用。

不像我预期的那样工作(没有错误,我想问题是没有“startActivity()”)所以我知道如何打开 com.hfade.appA.MainActivity 但没有:com.hfade.appA.ListActivity

你能帮助我吗?先感谢您

标签: androidandroid-intentnotifications

解决方案


嘿,我找到了解决方案-

Intent intent_anotherApp = new Intent(Intent.ACTION_MAIN);
intent_anotherApp.setComponent(new ComponentName("com.hfade.appA", "com.hfade.appA.ListActivity"));

所以它是关于 Intent.ACTION_MAIN 作为新 Intent 的 arg。


推荐阅读