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