Android service裡面啟動activity和alertdialog

Main-zy發表於2014-08-17

啟動activity原始碼:(記得要加上Intent.FLAG_ACTIVITY_NEW_TASK)

[java] view plaincopy
  1. Intent intent = new Intent();  
  2. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
  3. intent.setClass(getApplicationContext(),FileBrowserActivity.class);  
  4. startActivity(intent);  


啟動alertDialog原始碼:

[java] view plaincopy
  1.         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  2.         builder.setMessage("是否接受檔案?")  
  3.                 .setPositiveButton("是"new DialogInterface.OnClickListener() {  
  4.                     @Override  
  5.                     public void onClick(DialogInterface dialog, int which) {  
  6.   
  7.                     }  
  8.                 }).setNegativeButton("否"new OnClickListener() {  
  9.                     @Override  
  10.                     public void onClick(DialogInterface dialog, int which) {  
  11.                     }  
  12.                 });  
  13.         AlertDialog ad = builder.create();  
  14. //      ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); //系統中關機對話方塊就是這個屬性   
  15.         ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);  
  16.         ad.setCanceledOnTouchOutside(false);                                   //點選外面區域不會讓dialog消失  
  17.         ad.show();  
還要加上許可權

[html] view plaincopy
  1. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />  

相關文章