机器人在保存状态显示多个活动时,点击通知 [英] android displaying multiple activity in saved state when notification click

查看:166
本文介绍了机器人在保存状态显示多个活动时,点击通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的应用程序的通知。它工作在前景良好。当我真的在其他活动和preSS home键后,我单击通知。它打开给定的活动,但我需要保存状态显示previous活动。

I have a notification on my app. It works in foreground well. When i`m in a other activity and press home button after that i click notification. It opens given activity, but i need to display previous activity in a saved state.

        Notification note = new Notification(R.drawable.service_state_0,
            getResources().getText(R.string.serviceStarted),
            System.currentTimeMillis());
    Intent i = new Intent(this, StartingActivity_.class);

    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
    note.setLatestEventInfo(this,
            getResources().getText(R.string.app_name), getServiceStateDescription(CustomService.this), pi);
    note.flags |= Notification.FLAG_NO_CLEAR;
    note.flags |= Notification.FLAG_ONGOING_EVENT;

    startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID, note);

它总是显示StartingActivity,问题是,如何调用恢复活动单击通知时。如果活动尚未被破坏,我可以只调用该实例回(恢复它),因此无需再装回,不需要其他活动添加到我的筹码。这可能吗?

It always shows StartingActivity, the question is that how to call resuming activity when notification is clicked. If the activity has not already been destroyed and i can just call that instance back(resume it) and therefore not needing to load it again and won't need to add another activity to my stack. Is it possible?

推荐答案

我觉得像在开通知最近活动的解决方案:

I find a solution like open recently activity in Notification:

NotificationManager nm = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.flag = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;

Intent nIntent = new Intent();
nIntent = getPreviousIntent();

PendingIntent pi = PendingIntent.getActivity(this, 0, nIntent, 0);
notification.setLatestEventInfo(getContext(), "some Title", "some text", pi);
nm.notify(0,notification);


private Intent getPreviousIntent(Intent newIntent) {
    final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final List<ActivityManager.RecentTaskInfo> recentTaskInfos = am.getRecentTasks(1024,0);
    String myPkgNm = getPackageName();

    if (!recentTaskInfos.isEmpty()) {
        ActivityManager.RecentTaskInfo recentTaskInfo;
        for (int i = 0; i < recentTaskInfos.size(); i++) {
            recentTaskInfo = recentTaskInfos.get(i);
            if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(myPkgNm)) {
                newIntent = recentTaskInfo.baseIntent;
                newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
        }
    }
    return newIntent;
}

您必须添加&LT;使用许可权的android:NAME =android.permission.GET_TASKS/&GT;

这篇关于机器人在保存状态显示多个活动时,点击通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆