【问题标题】:Notification intent activity通知意图活动
【发布时间】:2013-10-08 04:11:31
【问题描述】:

我遇到了一些关于通知意图的问题。我有 2 项活动(A 和 B)。活动 A 是我的应用程序的主要活动。用户将经历的第一个活动。至于活动 B 是当用户通过单击活动 A 处的按钮进入时。

问题是当我单击通知时,它会将我引导至活动 B。然后将我引导回活动 A onBackPressed。但是当我关闭应用程序并通过多任务选项打开它时,它会在 Activity B 处恢复我的应用程序。我希望应用程序在关闭它之后从 Activity A 重新开始。

顺序应该是

活动 A --> 活动 B。

通知onClick -> 活动 B (onBackPressed) -> 活动 A --> 关闭。

重新打开应用/使用多任务功能打开 --> Activity A

如果有任何其他信息可以让我正确理解我的问题,请告诉我。

GCM_Intent.class

    Notification note = new Notification(icon, msgTopic ,System.currentTimeMillis());
    Intent i=new Intent(this, Activity_B.class);
    i.putExtra("topicId", topicId);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
    Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pi=PendingIntent.getActivity(this, CommunitiesappConstant.NOTIFICATION_ID, i,  PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    note.setLatestEventInfo(this, topicName ,msgInfo+message, pi);
    note.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
    note.ledARGB |= 0xff0000ff;
    note.ledOffMS |= 1000;
    note.ledOnMS |= 300;


    mgr.notify(CommunitiesappConstant.NOTIFICATION_ID, note);

活动 B.class

@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b);
    Bundle bundle = getIntent().getExtras();
    topicId = bundle.getString("topicId");

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Log.e(TAG,  "onBackPressed");
    Intent i = new Intent(Activity_B.this, Activity_A.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    finish();
}

【问题讨论】:

    标签: android android-intent android-activity notifications


    【解决方案1】:

    通知是您可以在应用程序的正常 UI 之外向用户显示的消息。这是使用通知的完美代码。

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!");
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ResultActivity.class);
    
    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ResultActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());
    

    【讨论】:

    • 我需要把这段代码放在哪里?当我在我的网站上发布任何新帖子时,我想向用户显示通知..我使用 WebView 创建了一个 android 应用程序
    【解决方案2】:

    这样做 onback press(),

    Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
            current.finish()
    

    【讨论】:

    • 你如何得到这个 mIdList.get(length-1)
    • mIdList.get(length-1) 只是您的第一个活动。试试吧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    相关资源
    最近更新 更多