我在清单中有一个exported=true
的活动。我目前正在做一些逻辑,看看活动是从 我的 应用的活动开始,还是从 另一个活动开始 应用。
让我担心的是,如果我在我的应用程序中设置了一个有意图的通知,那么通知点击启动的活动就属于“从外部启动”的逻辑。我遇到了问题,因为即使这是从“外部”开始,它也会被放入我当前的任务堆栈中。我可以通过一些意图标志解决这个问题(比如我假设的clear_top),但是我不希望其他应用程序创建通知来启动我的活动,因为它将被添加到我当前的任务堆栈中。
“通知”构建器文档说:
虽然操作是可选的,但您应该至少添加一个操作 你的通知。一个动作直接从用户那里获取 通知应用程序中的活动,他们可以查看 在导致通知或进一步工作的事件中。
要点是An action takes users directly from the
notification to an Activity in **your** application
。
奖金问题:另一个应用程序可以启动我的活动,并将该活动放在我的应用程序当前任务堆栈中吗?
编辑:我知道最简单的测试方法就是尝试一下,所以我创建了自己的示例应用程序来尝试测试它。它似乎不起作用。它不会启动活动(这很好),但它不会崩溃,它似乎根本没有做任何事情,所以我认为它可能不正确?
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);
}
}
答案 0 :(得分:0)
通知只需要一个PendingIntent。由于PendingIntent可以围绕任何意图构建,我认为你应该能够创建一个调用另一个应用程序的意图。更糟糕的情况是你创建了一个调用你的应用程序,它立即调用另一个。但我不认为这是必要的,我认为你可以直接提出你想要的意图。