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. publicvoid onClick(DialogInterface dialog, int which) {
  6. }
  7. }).setNegativeButton("否", new OnClickListener() {
  8. @Override
  9. publicvoid onClick(DialogInterface dialog, int which) {
  10. }
  11. });
  12. AlertDialog ad = builder.create();
  13. // ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); //系统中关机对话框就是这个属性
  14. ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  15. ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
  16. ad.show();
还要加上权限 
  1. "android.permission.SYSTEM_ALERT_WINDOW" />

你可能感兴趣的:(Android service里面启动activity和alertdialog .)