1.在广播中启动活动必须要加上:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

2.在广播中弹出弹出框,需要设置dialog的类型为:不然无法弹出

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

并且还要再配置文件中设置用户权限,因为上面表示这个弹出框是系统级别的了。

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

广播接收器完整代码:

public class ForceOfflineReceiver extends BroadcastReceiver

{

    @Override

    public void onReceive(final Context context, Intent intent)

    {

        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setTitle("Warning");

        builder.setMessage("You are forced to be offline,please to login again~");

        builder.setCancelable(false);

        builder.setPositiveButton("Ok"new OnClickListener()

        {

            @Override

            public void onClick(DialogInterface arg0, int arg1)

            {

                ActivityCollector.finishAll();

                Intent intent = new Intent(context, LoginActivity.class);

                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                context.startActivity(intent);

            }

        });

        //设置AlertDialog的类型,保证在广播接收器中可以正常弹出

        AlertDialog alertDialog = builder.create();

        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

        alertDialog.show();

    }

}

更多相关文章

  1. EditText 键盘输入类型
  2. Android(安卓)Switch控件(在android2.2 api8及以上使用)
  3. android接收短信(SmsMessage.createFromPdu((byte[])obj)不推荐使
  4. 2011.12.05(5)——— android JNI学习之四 JNI java c对应关系
  5. contentProvider操作
  6. C++与java混合编写和C++与OC混合编写 笔记
  7. android典型代码系列(二十一)------根据文件后缀名获得对应的MIM
  8. android intent 5.1
  9. The option 'android.enableAapt2' is deprecated and should no

随机推荐

  1. ListView嵌套ListView时发生:View too lar
  2. android APN的打开与关闭
  3. Android AIDL——实现机制浅析
  4. SurfaceView 的基本使用
  5. Android Studio 功能测评
  6. 自定义View系列教程06--详解View的Touch
  7. Android(安卓)Audio 数据流详解
  8. Android小技术知识(多用于面试)
  9. Android VR Player(全景视频播放器) [8]:Med
  10. Android(安卓)自定义密码输入框和键盘(仿