【问题标题】:Android: intent.putExtra in a Service Class to get notificationsAndroid:intent.putExtra 在服务类中获取通知
【发布时间】:2012-09-30 23:54:03
【问题描述】:

这是我在 Service 类中的 showNotification 方法:

private void showNotification() {

Notification notification = new Notification(R.drawable.icon,
"New Notification", System.currentTimeMillis());

Intent i = new Intent(this, myActivity.class);
i.putExtra("notification", "MyNotif");
i.putExtra("notifiedby", "NotedBy");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);
notification.setLatestEventInfo(this, "NotedBy", "MyNotif", contentIntent);
nm.notify(111, notification);
}

所以在点击状态栏中的Notification后,我将进入myActivity

问题是这些行总是在 myActivity 中给出 false

this.getIntent().hasExtra("notification")
this.getIntent().hasExtra("notifiedby")

putExtra() 不能与 PendingIntent 一起使用吗?

【问题讨论】:

    标签: android service android-intent


    【解决方案1】:

    试试这个代码:

    String notification = getIntent().getStringExtra("notification");
    String notifiedby = getIntent().getStringExtra("notifiedby");
    

    【讨论】:

    • 是的,即使我想通了。如果我删除“这个”,它工作正常。你知道为什么会这样吗?谢谢
    • 我不知道您是如何处理您的代码的,但有时“this”可能指的是错误的上下文。如果您不提供任何内容,android 将采用您当前活动的默认上下文,您会得到正确的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    相关资源
    最近更新 更多