監聽不到開機廣播的處理辦法

統計開機次數如果監聽不到開機廣播的處理辦法

  • 監聽關機廣播,這個是一定能夠監聽的到的
  • 在監聽到關機廣播時,隨便保存一條非空信息
  • 在Application中,獲取這條信息,判斷是否爲空
    1. 不爲空:次數+1,並把這條信息置空
    2. 爲空:不做處理
  • 廣播的監聽及次數處理結果如下:
//廣播
public class DeviceBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
            SharedPreferencesUtils.put(context,AppConfig.SHUTDOWN,AppConfig.SHUTDOWN);
            Log.i("廣播", "關機了");
        }

    }
}

//註冊
<!--關機廣播-->
<receiver
    android:name=".receiver.DeviceBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
        <category android:name="android.intent.category.HOME"></category>
    </intent-filter>
</receiver>

//次數處理
public class MyApplication extends Application {

    private int openCount=0;//開機次數

    @Override
    public void onCreate() {
        super.onCreate();
        String shutdown = (String) SharedPreferencesUtils.get(getApplicationContext(), AppConfig.SHUTDOWN, "");
        if (!TextUtils.isEmpty(shutdown)) {
            openCount = openCount + 1;
            SharedPreferencesUtils.put(getApplicationContext(),AppConfig.SHUTDOWN,"");
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章