我正在尝试创建一个可监听wifi状态更改并在更改之间的服务中运行倒数计时器的应用。如果我在活动处于前台时启用wifi,然后在清除最近的任务后禁用wifi,则会导致应用崩溃。崩溃后,它会向负责停止倒数计时器的方法抛出空指针异常。但是,如果我在清除最近的任务后同时启用和禁用,则该应用程序不会崩溃。
这是来自BroadcastReciever的OnReceive方法的代码,该方法负责处理WIFI_STATE_DISABLED状态:
case WifiManager.WIFI_STATE_DISABLED:
count++;
sharedPreferences.edit().putInt("Count", count).apply();
sharedPreferences.edit().putString(Integer.toString(count), "Disabled at -" + simpleDateFormat.format(new Date())).apply();
if (activityRunning) {
arrayList.add("Disabled at " + simpleDateFormat.format(new Date()));
arrayAdapter.notifyDataSetChanged();
textView.setText("Disabled");
aSwitch.setChecked(false);
}
if (context.getSharedPreferences("com.example.wifilimiter",MODE_PRIVATE).getBoolean("timerRunning", false)) {
stopTimer(context);
sharedPreferences.edit().putLong("millisLeft", millisleft).apply();
}
break;
上述上述StopTimer方法:
public static void stopTimer(Context context) {
if (context.getSharedPreferences("com.example.wifilimiter",MODE_PRIVATE).getBoolean("timerRunning", false)) {
countDownTimer.cancel(); // The app crushes here
context.getSharedPreferences("com.example.wifilimiter",MODE_PRIVATE).edit().putBoolean("timerRunning",false).apply();
Log.d("Timer Running : ","false");
}
这是CountDownTimer:
public static void startTimer(final Context context) {
context.getSharedPreferences("com.example.wifilimiter",MODE_PRIVATE).edit().putBoolean("timerRunning", true).apply();
countDownTimer = new CountDownTimer(millisleft, 1000) {
@Override
public void onTick(long millisUntilFinished) {
millisleft = millisUntilFinished;
String hms = String.format(Locale.getDefault(), "%02d:%02d:%02d", MILLISECONDS.toHours(millisleft),
MILLISECONDS.toMinutes(millisleft) - HOURS.toMinutes(MILLISECONDS.toHours(millisleft)),
MILLISECONDS.toSeconds(millisleft) - MINUTES.toSeconds(MILLISECONDS.toMinutes(millisleft)));
Toast.makeText(context, hms, Toast.LENGTH_SHORT).show();
if (context.getSharedPreferences("com.example.wifilimiter",MODE_PRIVATE).getBoolean("activityStatus",false)) {
Log.d(TAG, "onTick: " + hms);
timer.setText(hms);
}
}
@Override
public void onFinish() {
}
}.start();
}
这是下面的日志:
Process: com.example.wifilimiter, PID: 5306
java.lang.RuntimeException: Unable to start receiver com.example.wifilimiter.BroadcastReceiver: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2424)
at android.app.ActivityThread.access$1700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.wifilimiter.TimerService.stopTimer(TimerService.java:67) //This refers to the countDownTimer.cancel() method
at com.example.wifilimiter.BroadcastReceiver.onReceive(BroadcastReceiver.java:85)//This refers to the StopTimer() method
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2417)
at android.app.ActivityThread.access$1700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
当您从最近任务列表中删除该应用程序时,托管该应用程序的OS进程将被杀死。您的计时器已死。
您需要通过检查onReceive()
是否为countDownTimer
来检查计时器是否在null
中运行。如果是这样,则您的计时器没有在运行,您不再需要停止它。