- service中创建系统Dialog
public void showDialog(String msgBody) {
String msg = msgBody.substring(8, msgBody.length());
Dialog dialog = new Dialog(this, R.style.NoBgDialog, msg);
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.setCanceledOnTouchOutside(true);
if (Build.VERSION.SDK_INT < 23 || (Build.VERSION.SDK_INT >= 23 && Settings.canDrawOverlays(this))) {
dialog.show();
} else {
Intent intent;
switch (Build.MANUFACTURER) {
case "Meizu":
intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setClassName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("packageName", this.getPackageName());
break;
default:
intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:com.test"));
break;
}
try {
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
Log.e(THIS_FILE,e.getMessage().toString());
}
}
}
- handler中创建Dialog需要注意上下文
private static class NetWorkCheckHandler extends Handler {
private WeakReference<Context> reference;
public NetWorkCheckHandler(Context context) {
reference = new WeakReference<>(context);
}
@Override
public void handleMessage(Message msg) {
final FirstActivity activity = (FirstActivity ) reference.get();
if (activity != null) {
switch (msg.what) {
case connectionInvaild:
AlertDialog connectionInvalidDialog = MyDialog.showOneButtonDialog(activity,
null, activity.getResources().getColor(R.color.white),
activity.getResources().getString(R.string.set_connection_then_open_mptt),
activity.getResources().getColor(R.color.white),
new View.OnClickListener() {
@Override
public void onClick(View v) {
activity.finish();
}
});
if (connectionInvalidDialog != null) {
connectionInvalidDialog.setCancelable(false);
}
}
}
}
}
dialog如果在handler等内部类中创建,需要调用context上下文,不能使用hander作为上下文,需要传递activity作为上下文。