android如何在用户单击通知时开始活动? [英] android How to start activity when user clicks a notification?

查看:82
本文介绍了android如何在用户单击通知时开始活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户单击通知时打开活动.我知道这个问题是重复的,但是找不到解决方法,这就是我所做的

I wanna open activity when user clicks a notification. I know this question is duplicated but couldn`t find a solution here is what i did

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");

Intent resultIntent = new Intent(this, ResultActivity.class);

// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
);

mBuilder.setContentIntent(resultPendingIntent);
// Sets an ID for the notification
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());

我遵循的是文档.有人有什么主意吗?为什么ResultActivity无法打开?

Here is documentation i followed. Does anyone have any idea? why ResultActivity couldn't open?

推荐答案

这是我的最终解决方案:

Here is my final solution:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(YourService.this)
            .setContentTitle(getResources().getText(R.string.app_name))
            .setContentText(getServiceStateDescription(HomeBridgeService.this))
            .setSmallIcon(iconId)
            .setWhen(System.currentTimeMillis());

    Intent nIntent = getPreviousIntent();
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack
    stackBuilder.addParentStack(MainActivity_.class);
    stackBuilder.addNextIntent(nIntent);
    PendingIntent pendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.setContentIntent(pendingIntent);

    startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID, notificationBuilder.build());


private Intent getPreviousIntent() {
    Intent newIntent = null;
    final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        final List<ActivityManager.AppTask> recentTaskInfos = activityManager.getAppTasks();
        if (!recentTaskInfos.isEmpty()) {
            for (ActivityManager.AppTask appTaskTaskInfo: recentTaskInfos) {
                if (appTaskTaskInfo.getTaskInfo().baseIntent.getComponent().getPackageName().equals(ContextConstants.PACKAGE_NAME)) {
                    newIntent = appTaskTaskInfo.getTaskInfo().baseIntent;
                    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
            }
        }
    } else {
        final List<ActivityManager.RecentTaskInfo> recentTaskInfos = activityManager.getRecentTasks(1024, 0);
        if (!recentTaskInfos.isEmpty()) {
            for (ActivityManager.RecentTaskInfo recentTaskInfo: recentTaskInfos) {
                if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(ContextConstants.PACKAGE_NAME)) {
                    newIntent = recentTaskInfo.baseIntent;
                    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
            }
        }
    }
    if (newIntent == null) newIntent = new Intent();
    return newIntent;
}

这篇关于android如何在用户单击通知时开始活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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