首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通知无法转到当前活动

通知无法转到当前活动
EN

Stack Overflow用户
提问于 2014-04-02 18:20:46
回答 2查看 234关注 0票数 1

在我的通知中,我希望在单击待定意向时启动当前活动。在谷歌中,找到了很多方法,我也尝试了很多次,但都不起作用。这是我的代码,

代码语言:javascript
运行
复制
          public void onStart(Intent intent, int startId)
   {
     super.onStart(intent, startId);

     String startTime = intent.getStringExtra("Strar_time");

     NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        long when = System.currentTimeMillis();         // notification time
        Notification notification = new Notification(R.drawable.ic_launcher, "reminder", when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= notification.FLAG_AUTO_CANCEL;
        notification.vibrate = new long[]{100, 200, 100, 500};
        notification.defaults = Notification.DEFAULT_ALL;

        Intent notificationIntent = new Intent(this, Goo.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP/* | Intent.FLAG_ACTIVITY_SINGLE_TOP*/);

        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent , 0);
        notification.setLatestEventInfo(getApplicationContext(), "It's about time", "Your time is "+startTime, contentIntent);
        notification.contentIntent=contentIntent;
        nm.notify(NOTIF_ID, notification);
        NOTIF_ID++;
        Log.i("NotiID",NOTIF_ID+"");
    }
EN

Stack Overflow用户

发布于 2014-04-02 18:37:54

尝试下面给出的函数,

代码语言:javascript
运行
复制
private static void generateNotification(Context context, String message) {
    int icon = R.drawable.launch_icon;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = null;

    notificationIntent = new Intent(context, Main.class);

    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);

}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22808358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档