我有一个活动,它在清单中有exported=true
。我目前正在做一些逻辑,看看活动是从我的应用程序的活动开始的,还是从另一个应用程序的活动开始的。
让我担心的是,如果我在我的应用程序中设置了一个带有意图的通知,那么由通知点击启动的活动就会落入我的"从外部启动"的逻辑。我遇到了一个问题,因为即使这是从"外部"开始的,它也会被放入我当前的任务堆栈中。我可以修复这个与一些意图标志,(像clear_top我假设),但我不希望另一个应用程序创建一个通知来启动我的活动,因为它会被添加到我当前的任务堆栈。
通知构建器文档说:
虽然操作是可选的,但您应该至少添加一个操作到你的通知。操作直接从控件获取用户通知到应用程序中的Activity,它们可以查看在引起通知的事件中,或执行进一步的工作。
重点是An action takes users directly from the
notification to an Activity in **your** application
。
EDIT:我知道测试某事最简单的方法就是尝试它,所以我创建了我自己的示例应用程序来尝试测试它。但它似乎不起作用。它没有启动活动(这是好的),但它不会崩溃,它似乎没有做任何事情,所以我认为这可能是不正确的?
public class MainActivity extends Activity {
Intent intent;
PendingIntent resultPendingIntent;
Intent resultIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
LinearLayout layout = (LinearLayout) findViewById(R.id.rootView);
Button button1 = new Button(this);
button1.setText("Send notification");
intent = new Intent();
resultIntent = new Intent();
intent.setComponent(new ComponentName("com.myapp.tester",
"com.myapp.tester.MyMainActivity"));
final PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
});
layout.addView(button1);
}
}
通知只是取一个PendingIntent。由于PendingIntent可以围绕任何intent构建,我认为你应该能够创建一个调用另一个应用程序的PendingIntent。更糟糕的情况是,您创建了一个调用您的应用程序的应用程序,该应用程序立即调用另一个应用程序。但我不认为这是必要的,我认为你可以直接输入你想要的意图