android service启动AlertDialog

项目中是接收系统广播启动service,然后在service中弹出列表样式的AlertDialog。

1.广播启动service,intent传递路径

Intent i = new Intent(context, UpdateSystemService.class);
i.putExtra("update_path",updatePath);
context.startService(i);

2.service中初始化AlertDialog并作为系统弹框显示出来

AlertDialog.Builder builder = new AlertDialog.Builder(mContext,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
 
builder.setCancelable(false);
builder.setTitle(R.string.alert_update_title);
builder.setMultiChoiceItems(mUpdateFile,mCheck,null); 

builder.setPositiveButton(R.string.alert_confirm_update_ok, null);
builder.setNegativeButton(R.string.alert_confirm_update_cancel, null); 

        mAlertDialog = builder.create();//(create()一定要在所有的builder属性设置完以后才可以,否则无法显示)
mAlertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
mAlertDialog.setCanceledOnTouchOutside(false);
mAlertDialog.show();

3.AndroidManifest.xml中添加

  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

   <service android:name="com.cyanogenmod.filemanager.ui.dialogs.UpdateSystemService">
   </service>

猜你喜欢

转载自blog.csdn.net/yhyqf/article/details/78331341