zoukankan      html  css  js  c++  java
  • Android广播接收器里弹出对话框

    不多说,直接上车。。。

     1 public class MyReceiver extends BroadcastReceiver {
     2     @Override
     3     public void onReceive(final Context context, Intent intent) {
     4         AlertDialog.Builder builder = new AlertDialog.Builder(context);
     5         builder.setTitle("提示");
     6         builder.setMessage("确定打开主界面吗?");       
     7         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
     8             @Override
     9             public void onClick(DialogInterface dialog, int which) {
    10                 Intent intent1 = new Intent(context, MainActivity.class);
    11 
    12                 //在广播接收器中启动活动,一定要给Intent加入FLAG_ACTIVITY_NEW_TASK标志
    13                 intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    14                 context.startActivity(intent1);
    15             }
    16         });
    17 
    18         AlertDialog dialog = builder.create();
    19 
    20         //需要把对话框的类型设为TYPE_SYSTEM_ALERT,否则对话框无法在广播接收器里弹出
    21         dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    22         dialog.show();
    23     }
    24 }

    注意:把对话框的类型设为了TYPE_SYSTEM_ALERT, 这样弹出的就是一个系统级别的对话框,因此必须声明android.permission.SYSTEM_ALERT_WINDOW权限。最后不要忘记注册广播接收器哦。

  • 相关阅读:
    UI自动化测试框架
    Pyse( selenium-api的二次封装)
    Selenium之webdriverAPI
    selenium 基础之定位方式
    html综合应用
    html基础之js操作
    html基础之Dom操作
    (九)Locust 参数化
    (八)Locust 设置断言
    (七)Locust 的类和方法
  • 原文地址:https://www.cnblogs.com/nangch/p/5353401.html
Copyright © 2011-2022 走看看