事业的PendingIntent错误 [英] PendingIntent cause Error

查看:172
本文介绍了事业的PendingIntent错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个BroadcastReceiver它通过意向从活动中获取数据,用的PendingIntent。在code做工精细,但是当我重新启动我的设备和调用的onReceive我得到的错误...
我不知道是什么错误,因为他后我的手机重启出现,logchat无法看到电话,我没有看到错误...

I have a BroadcastReceiver which gets data from Activity by Intent, with PendingIntent. The code work fine but when i'm restarting my device and onReceive calling i am getting Error... I don't know what the error because he appear after my phone restarting and the logchat cannot notice the phone and i don't see the error...

活动:

 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_CANCEL_CURRENT仍然没有改变。
感谢您的帮助。

I tried change the FLAG to FLAG_CANCEL_CURRENT still nothing changed. Thanks for helping.

推荐答案

我想,可能是那里有错误出现,由于没有活动启动后重新启动您的手机和接收器仍然尝试接收您的字符串的邮件。

I think, may be there where error comes due to activity not starting after restart your phone and receiver still try to receive your string messages.

我不知道,你怎么可以解决你的这个问题。当你的手机重新启动接收端收到空值。尝试使用共享preference或字符串的缓冲区来存储你的接收机字符串值。

I don't know, How you can solve your this problem. when your phone restart your receiver gets null value. Try to use shared preference or String buffer to store your receiver String value.

删除此code

     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);

和指定清单文件这个动作是你的接收机这样的

and specify this action in manifest file which is your receiver like this

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

现在让改变你的接收器code。与code

Now make change your receiver code with code

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 ,根据以上code您共享preference字符串获取和意图传递给 alarmDialog.class 当您的设备启动完成。

You have set action Intent.ACTION_BOOT_COMPLETED, With above code you your sharedpreference string get and pass with intent to alarmDialog.class when your device boot is complete.

您已经指定在manifest文件中这个动作所以现在没有必要做第二次。试试这个code。
试试这个。

you have specified this action in your manifest file so now no need to do second time. try this code. Try this .

这篇关于事业的PendingIntent错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆