从另一个应用程序启动非主要活动

时间:2019-11-26 19:31:11

标签: android android-intent notifications

我有两个申请。假设: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());
}

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

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"));

在pendingActivity中为arg无效。

无法正常工作(没有错误,我想问题是没有“ startActivity()”) 所以我知道如何打开com.hfade.appA.MainActivity,但没有:com.hfade.appA.ListActivity

你能帮我吗?预先谢谢你

1 个答案:

答案 0 :(得分:0)

嘿,我找到了解决方法-

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。

相关问题