我尝试了多种方法,但都不起作用。我不知道为什么这么复杂,在文档中看起来很容易! 我想通过通知(用户在通知栏中点击它)触发OnNewIntent。
目前,我已将我的Activity设置为singleTop。
<activity
android:name="-.-.-.MainMenuActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
</activity>
这是我创建通知的代码:
public void showNotification(String text, String componentId){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Title")
.setAutoCancel(true)
.setContentText(text);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainMenuActivity.class);
if(!componentId.equalsIgnoreCase("")){
resultIntent.putExtra("componentId", componentId);
}
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
mBuilder.setFullScreenIntent(resultPendingIntent, false);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
这是我MainMenuActivity中的OnNewIntent方法:
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
setIntent(intent);
...
}
我从来没有收到OnNewIntent的调用。我不知道我做错了什么。整个应用程序中我只使用了2个活动,MainMenuActivity在LoginActivity之后,因此MainMenuActivity应该始终位于堆栈顶部(我有更多片段,在其中我替换了MainMenuActivity内部的片段)。
任何帮助都将不胜感激!谢谢大家。