【问题标题】:Notification from a service works, but the activity is not started来自服务的通知有效,但活动未启动
【发布时间】:2011-11-18 21:47:12
【问题描述】:

我正在尝试从服务发送通知,并且成功发送(通知确实显示在状态栏上)。 但是,在单击通知以观看活动后 - 我将返回移动主屏幕。此外,我注意到在调试模式下我没有达到活动的“onCreate”方法。

这是我的一段代码: 服务代码:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    int icon = R.drawable.notificationicon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, NotificationsActivity.class), 0);
    notification.setLatestEventInfo(this, "My notification","Hello World!", pendingIntent);
    notificationManager.notify(1, notification);

活动代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(dialogLayout);
}

【问题讨论】:

  • AndroidManifest.xml 中的 NotificationActivity 类是否定义良好? (你能粘贴你的 AndroidManifest 吗?)

标签: android android-layout android-emulator android-service android-ui


【解决方案1】:

如果你想从接收器/服务中startActivity,你必须创建一个意图,添加一个标志通知它开始一个新任务,然后开始活动。

修改这个伪代码:

Intent startServiceIntent = new Intent(context, com.myCorp.myApp.myClass.class);
startServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startServiceIntent);

我在 BootReceiver 中使用它来启动应用程序 (for example use this BootReceiver)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    相关资源
    最近更新 更多