我希望在服务检查没有互联网连接时禁用屏幕并显示进度对话框 这是我的代码
public class Wifiservice extends Service {
public static final int notify = 5000; //interval between two services(Here Service run every 5 Minute)
private Handler mHandler = new Handler(); //run on another Thread to avoid crash
private Timer mTimer = null; //timer handling
public static final int alert_notify = 50; //interval between two services(Here Service run every 5 Minute)
private Handler alert_mHandler = new Handler(); //run on another Thread to avoid crash
private Timer alert_mTimer = null; //timer handling
AlertDialog dialog;
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
if (mTimer != null) // Cancel if already existed
mTimer.cancel();
else
mTimer = new Timer();
dialog = new AlertDialog.Builder(Wifiservice.this).create();
//recreate new
mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify); //Schedule task
if (alert_mTimer != null) // Cancel if already existed
alert_mTimer.cancel();
else
alert_mTimer = new Timer();
//recreate new
alert_mTimer.scheduleAtFixedRate(new hide_alert_box(), 0, alert_notify); //Schedule task
}
@Override
public void onDestroy() {
super.onDestroy();
mTimer.cancel(); //For Cancel Timer
}
//class TimeDisplay for handling task
class TimeDisplay extends TimerTask {
@Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
@Override
public void run() {
if(!isAppIsInBackground(getApplicationContext())) {
boolean flage = GlobalClass.isDeviceOnline(getApplicationContext());
if (!flage && GlobalClass.wififlage == 1) {
GlobalClass.wififlage = 0;
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
dialog.getWindow().setGravity(Gravity.CENTER);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
LayoutInflater factory = LayoutInflater.from(Wifiservice.this);
final View view = factory.inflate(R.layout.activity_waiting_for__network, null);
dialog.setView(view);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
if(!isAppIsInBackground(getApplicationContext())) {
if (!GlobalClass.isDeviceOnline(getApplicationContext())) {
dialog.show();
}
}
else{
GlobalClass.wififlage = 1;
}
}
});
dialog.show();
} else if (flage && GlobalClass.wififlage == 0) {
GlobalClass.wififlage = 1;
dialog.cancel();
}
}
}
});
}
}
class hide_alert_box extends TimerTask {
@Override
public void run() {
alert_mHandler.post(new Runnable() {
@Override
public void run() {
if(isAppIsInBackground(getApplicationContext())) {
GlobalClass.wififlage = 1;
dialog.cancel();
}
}
});
}
}
private boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}
return isInBackground;
}
}
显示以下错误,而不是应用崩溃
Process: , PID: 20161
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@1511bfc -- permission denied for window type 2003
at android.view.ViewRootImpl.setView(ViewRootImpl.java:914)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:377)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:97)
at android.app.Dialog.show(Dialog.java:404)
at com.houzcalls.services.Wifiservice$TimeDisplay$1.run(Wifiservice.java:106)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)