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();
}
}