Android service里面启动activity和alertdialog .

启动activity源码:(记得要加上Intent.FLAG_ACTIVITY_NEW_TASK)

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

启动alertDialog源码:

   
   
   
   
  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(); 
还要加上权限
   
   
   
   
  1. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 

 

你可能感兴趣的:(源码,android,service,Activity)