【问题标题】:Launch not main activity from another application从另一个应用程序启动不是主要活动
【发布时间】:2020-03-22 07:11:10
【问题描述】:

我有两个应用程序。假设: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

你能帮帮我吗?提前谢谢你

【问题讨论】:

    标签: android android-intent notifications


    【解决方案1】:

    呵呵,我找到了解决办法——

    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。

    【讨论】:

      猜你喜欢
      • 2013-02-17
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 2017-01-08
      • 2012-06-17
      相关资源
      最近更新 更多