android - 从另一个应用程序启动不是主要活动
问题描述
我有两个申请。假设: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
你能帮助我吗?先感谢您
解决方案
嘿,我找到了解决方案-
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。
推荐阅读
- reactjs - 情感 css 功能未呈现样式(React)
- python - 求解一对迭代微分方程
- dart - 为什么 Dart 只允许在初始化列表上启动 Final 变量?
- php - 删除 WooCommerce 订单上的美分(四舍五入到最接近的美元)
- javascript - GetJSON 方法仅拉取数组而不拉取特定字段
- javascript - JSON 对象 - JavaScript、C#、MVC
- javascript - html2pdf:如何隐藏从PDF?
- android - Xamarin 使用 inflater 内部循环动态添加视图,仅添加一个条目
- java - 如何集成 loopj 以进行 json 解析
- signalr - 在打开应用洞察时连接到我的 SignalR Web 应用时出现 502 错误网关