模拟在Service中弹出系统对话框
- SystemDialog
public abstract class BaseDialog extends Dialog {
public BaseDialog(Context context) {
super(context, R.style.dialog_style);
setContentView(getLayoutId());
ButterKnife.bind(this);
}
protected abstract int getLayoutId();
}
public class SystemDialog extends BaseDialog {
@BindView(R.id.tv_book_content)
TextView mTvContent;
@BindView(R.id.btn_book_positive)
Button mBtnPositive;
@BindView(R.id.btn_book_negative)
Button mBtnNegative;
private OnCommPositiveListener mOnPositiveListener;
private OnCommNegativeListener mOnNegativeListener;
@OnClick(R.id.btn_book_positive)
void positive() {
dismiss();
if (mOnPositiveListener != null) {
mOnPositiveListener.onPositiveListener();
}
}
@OnClick(R.id.btn_book_negative)
void negative() {
dismiss();
if (mOnNegativeListener != null) {
mOnNegativeListener.onNegativeListener();
}
}
public BookReadyDialog(Context context) {
super(context);
Window window = getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = (int) (context.getResources().getDisplayMetrics().widthPixels * 0.5);
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
setCanceledOnTouchOutside(false);
setCancelable(false);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
@Override
protected int getLayoutId() {
return R.layout.dialog_book_ready_layout;
}
public BookReadyDialog content(String content) {
mTvContent.setText(TextUtils.isEmpty(content) ? "" : content);
return this;
}
public BookReadyDialog channelName(String channelName) {
mTvChannelName.setVisibility(TextUtils.isEmpty(channelName) ? View.GONE : View.VISIBLE);
mTvChannelName.setText(TextUtils.isEmpty(channelName) ? "" : MessageFormat.format(getContext().getString(R.string.dialog_book_ready_channel_name), channelName));
return this;
}
public BookReadyDialog mode(String mode) {
mTvMode.setText(TextUtils.isEmpty(mode) ? "" : MessageFormat.format(getContext().getString(R.string.dialog_book_ready_mode), mode));
return this;
}
public void updateContent(String content) {
mTvContent.setText(content);
}
public BookReadyDialog setOnPositiveListener(String positive, OnCommPositiveListener listener) {
mBtnPositive.setText(TextUtils.isEmpty(positive) ? getContext().getString(R.string.yes) : positive);
this.mOnPositiveListener = listener;
return this;
}
public BookReadyDialog setOnNegativeListener(String negative, OnCommNegativeListener listener) {
mBtnNegative.setText(TextUtils.isEmpty(negative) ? getContext().getString(R.string.cancel) : negative);
this.mOnNegativeListener = listener;
return this;
}
}
- SystemDialogService
public abstract class BaseService extends Service {
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.createNotificationChannel(
new NotificationChannel(getPackageName(), getPackageName(),
NotificationManager.IMPORTANCE_DEFAULT));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getPackageName());
startForeground(1, builder.build());
}
}
@Override
public void onDestroy() {
stopForeground(true);
super.onDestroy();
}
public static void bootService(Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
MyApplication.getContext().startForegroundService(intent);
} else {
MyApplication.getContext().startService(intent);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
public class SystemDialogService extends BaseService {
private SystemDialog mSystemDialog;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
private void showSystemDialog() {
mSystemDialog = new SystemDialog(this)
.content("Tips")
.setOnPositiveListener("ok", new OnCommPositiveListener() {
dismissSystemDialog();
})
.setOnNegativeListener("cancel", new OnCommNegativeListener() {
dismissSystemDialog();
});
// 兼容8.0以上弹出系统对话框
// 需要注意的是,如果在service中在某一个系统对话框弹出后再弹出其他系统对话框,会将上一个销毁再弹出当前的系统对话框
if (mSystemDialog.getWindow() != null) {
mSystemDialog.getWindow().setType(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
mSysetmDialog.show();
}
private void dismissSystemDialog() {
if (mSystemDialog != null && mSystemDialog.isShowing()) {
mSystemDialog.dismiss();
mSystemDIalog = null;
}
}
}
- AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
// service放置前台
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />