【问题标题】:Intent missing only some extras in IntentServiceIntent 仅缺少 IntentService 中的一些附加功能
【发布时间】:2016-06-13 06:23:55
【问题描述】:

这几天我一直在为此烦恼,所以我希望有人能指出我做错了什么。我正在创建一个带有 5 个附加字符串的意图,并将其设置为 PendingIntent 以与 AlarmManager 一起使用。但是,当警报触发并且我继续处理我的服务类的 onHandleIntent 中的意图时,意图中只有 3 个额外内容。下面是一些代码和屏幕截图。

意图创建:

    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
        ;
        notificationIntent.addCategory("android.intent.category.DEFAULT");
        notificationIntent.putExtra("key1", "val5");
        notificationIntent.putExtra("key2", "val4");
        notificationIntent.putExtra("key3", "val3");
        notificationIntent.putExtra("key4", "val4");
        notificationIntent.putExtra("key5", "val5");
        return notificationIntent;

待创建意图:

PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getActivity(), Integer.parseInt(obj.uniqueId), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Intent 服务:

protected void onHandleIntent(Intent intent) {

        if(intent != null) {

            if (intent.getExtras() != null) {

             String val1 = intent.getStringExtra("key1");
             String val2 = intent.getStringExtra("key2");
             String val3 = intent.getStringExtra("key3");
             String val4 = intent.getStringExtra("key4");
             String val5 = intent.getStringExtra("key5");

             // proceed with other stuff..

      }
   }
}

基于对与我类似的问题的其他答案,我也尝试过:

final Intent originalIntent = (Intent)intent.getExtras().get( Intent.EXTRA_INTENT );
final String val1 = originalIntent.getStringExtra("key1");

但 originalIntent 始终为空。我不明白为什么只有 3 个附加功能可用,而其他 2 个不可用。它们都是字符串值,所以我无法理解这种歧视:(

调试器截图: 创建意图时 -

在意图服务类中读取意图时 -

我也尝试按照 SO 的一些答案中的建议执行 intent.setAction("",Math.random());,但这导致警报根本没有触发。 非常感谢任何帮助。

【问题讨论】:

  • 如果你把它改成PendingIntent pendingIntent = PendingIntent.getActivity(this, Integer.parseInt(obj.uniqueId), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 呢?
  • @ShreeKrishna:执行 getActivity 导致警报根本没有触发
  • obj.uniqueId 的值是多少?它真的是独一无二的还是总是一样的?
  • @pskink:唯一的 id 对那个对象来说是唯一的,就像一个 id。它用于在警报触发时在 Intent 服务中设置特定 obj 的一些属性。
  • 好吧,我错过了Intent.EXTRA_INTENT,它是做什么用的?只需使用intent.getStringExtra("key1")intent.getStringExtra("key2")

标签: android android-intent alarmmanager android-pendingintent intentservice


【解决方案1】:

您已将“附加”添加到广播Intent,然后您声称在您的Service 中只有 5 个出现。如何查看从onReceive() 中的广播Intent 中提取“附加”并将它们复制到您用来调用startService()Intent 的代码。我想你会发现你的问题就在那里:-)

【讨论】:

    猜你喜欢
    • 2012-06-12
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多