【问题标题】:PendingIntent cause ErrorPendingIntent 导致错误
【发布时间】:2015-06-12 15:04:44
【问题描述】:

我有一个 BroadcastReceiver,它使用 PendingIntent 从 Intent 获取数据。代码工作正常,但是当我重新启动设备并 onReceive 调用时,我收到错误... 我不知道是什么错误,因为他在我的手机重新启动后出现,并且 logchat 无法注意到手机并且我没有看到错误...

活动:

 Intent intent = new Intent(addOne.this,AlarmReceiver.class);
 intent.putExtra("msg", title.getText().toString());
 intent.putExtra("note", note.getText().toString());

AlarmManager alarmMgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);

接收者:

String msg=intent.getStringExtra("msg");
String note=intent.getStringExtra("note");

Intent startIntent = new Intent(context, alarmDialog.class);                                
startIntent.putExtra("msg",msg);
startIntent.putExtra("note",note);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);

清单:

    <receiver android:name=".AlarmReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

我尝试将 FLAG 更改为 FLAG_CANCEL_CURRENT 仍然没有任何改变。 感谢您的帮助。

【问题讨论】:

    标签: android android-pendingintent


    【解决方案1】:

    我认为,可能是由于重新启动手机后活动未开始而导致错误出现,并且接收器仍尝试接收您的字符串消息。

    我不知道,您如何解决这个问题。当您的手机重新启动时,您的接收器将获得空值。尝试使用共享首选项或字符串缓冲区来存储您的接收者字符串值。

    删除此代码

         Intent intent = new Intent(addOne.this,AlarmReceiver.class);
         intent.putExtra("msg", title.getText().toString());
         intent.putExtra("note", note.getText().toString());
    

    试试这个

          Intent i = new Intent("my.action.string");               
          i.putExtra("msg",title.getText().toString());
          i.putExtra("note", note.getText().toString());
          sendBroadcast(i);
    

    并在清单文件中指定此操作,这是您的接收器,就像这样

    <receiver android:name=".AlarmReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="my.action.string" />
        </intent-filter>
    </receiver>
    

    现在用代码更改您的接收器代码

    public void onReceive(Context context, Intent intent)
    {
           if(intent.getAction().equals("my.action.string"))
           {     
    
           SharedPreferences  sharedpreferences = context.getSharedPreferences(MyPREFERENCESS, context.MODE_PRIVATE);
        // get your intent which you have passed in receiver 
    
          String msg=intent.getStringExtra("msg");
       //  Now save your string with shared preference
          sharedpreferences.edit().putString("msg1",msg).commit();
    
          String note=intent.getStringExtra("note");
          sharedpreferences.edit().putString("note1",note).commit();
         }
    
     // now get your sharedpreference String  with boot complete
    
     // and start activity with passing your string data
    
         if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
         {
            // get the sharedpreference string and pass it with intent
            // to alarmDialog.class
    
            String message = sharedpreference.getString("msg1",msg1);          
            String notes = sharedpreference.getString("note1",note1);
            Intent startIntent = new Intent(context, alarmDialog.class);                                
            startIntent.putExtra("msg",message);
            startIntent.putExtra("note",notes);
    
            //startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
            context.startActivity(startIntent);
    
       }`
    }`
    

    您已设置操作 Intent.ACTION_BOOT_COMPLETED,使用上述代码,您的 sharedpreference 字符串将在您的设备启动完成时有意地获取并传递给 alarmDialog.class

    您已在清单文件中指定此操作,因此现在无需第二次执行。试试这个代码。 试试这个。

    【讨论】:

    • 我认为你是对的,这就是我的想法。但我无法将它们存储在 SharedPreference 中,因为我有一些警报...该应用程序是 TO DO LIST ...我正在失去理智,我该怎么办?谢谢。
    • 也许我可以通过 setAction 移动我的字符串并通过 getAction 将它们放入接收器?
    • 没看懂代码的意思... Intent i1= new Intent(context, PendingIntent.class); i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);新意图(“android.intent.action.MAIN”);你确定代码有帮助吗?我看到它只是过滤异常
    • 它接收 Pendingintent 类的上下文并在屏幕打开的情况下启动 Pending 活动。
    • 现在我看到应用程序每次打开手机后都会出错......这与接收器无关......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多