統計開機次數如果監聽不到開機廣播的處理辦法
- 監聽關機廣播,這個是一定能夠監聽的到的
- 在監聽到關機廣播時,隨便保存一條非空信息
- 在Application中,獲取這條信息,判斷是否爲空
- 不爲空:次數+1,並把這條信息置空
- 爲空:不做處理
- 廣播的監聽及次數處理結果如下:
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,"");
}
}
}