Android service里面启动activity和alertdialog

来源:互联网 发布:在中国卖的好跑车知乎 编辑:程序博客网 时间:2024/05/16 06:22
启动activity源码:(记得要加上Intent.FLAG_ACTIVITY_NEW_TASK)

[java] view plaincopyprint?
  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 plaincopyprint?
  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 plaincopyprint?
  1. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />  
原创粉丝点击