开发者

Why isn't my ON_BOOT_COMPLETED Broadcast Receiver working?

Trying to reset a SharedPreference every time the device is rebooted.

Here's my code:

MyReceiver.java

   package ***************;

    import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class MyReceiver extends BroadcastReceiver {
    SharedPreferences mPrefs;

    @Override
    public void onReceive(Context context, Intent intent) {

        mPrefs  = context.getSharedPreferences("myAppPrefs", 0); 
        setStatus("***********");


    }

     public void setStatus(String statustext) {

         SharedPreferences.Editor edit = mPrefs.edit();
         edit.putString("status", statustext);
         edit.commit();
      }

}

Android Manifest

     <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              android:versionCode="1" package="*********" android:versionName="1.8.3">
                  <application android:icon="@drawable/icon" android:label="@string/app_name">
                <activity android:name=".Main"
                          android:label="@string/app_name"
                          android:configChanges="keyboardHidden|orientation"                                 
    android:screenOrientation="portrait">
                         <intent-filter>
                        <action android:name="android.intent.action.MAIN" />
                        <category android:name="android.intent.category.LAUNCHER" />
    开发者_运维技巧                </intent-filter>
                </activity>

                <activity android:name="help"></activity>
    <activity android:name="MyReceiver"></activity>
    <receiver android:name=".MyReceiver">

                <intent-filter> 
                        <action android:name="android.intent.action.BOOT_COMPLETE"/> 
                        <category android:name="android.intent.category.DEFAULT" /> 
                </intent-filter> 
  </receiver>
</application>
    <uses-sdk android:minSdkVersion="4" />


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

</manifest> 

The SharedPreferences are setup in my Main activity. My goal is to get the ON_BOOT_COMPLETED broadcast to trigger the MyReceiver activity, which resets a specific sharedPreference, however, when I try this, the SharedPref is not reset.

What am I doing wrong? (I incorporated both @Commonware and @Blundell's fixes, but it is still not working)


Your not retrieving your shared preferences.

SharedPreferences mPrefs;
SharedPreferences.Editor edit = mPrefs.edit();

Does nothing. You'd need:

@Override
public void onReceive(Context context, Intent intent) {

    mPrefs  = context.getSharedPreferences(); // or if your not using the default  getSharedPreferences (String name, int mode)
    setStatus("****************");


}


You have a <receiver> element outside of the <application> element, which will be ignored.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜