1. 程式人生 > >android service啟動AlertDialog

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>