android广播中实现弹出窗口
来源:互联网 发布:讨鬼传2最美捏脸数据 编辑:程序博客网 时间:2024/04/30 21:06
配置的权限:<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>public class MainActivity extends AppCompatActivity {Handler mHandle; Button mButton; private BroadcastReceiver mRecemiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //检测API是不是小于23,因为到了API23之后getNetworkInfo(int networkType)方法被弃用 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { //获得ConnectivityManager对象 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //获取ConnectivityManager对象对应的NetworkInfo对象 //获取WIFI连接的信息 NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); //获取移动数据连接的信息 NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) { Toast.makeText(context, "WIFI已连接,移动数据已连接", Toast.LENGTH_SHORT).show(); } else if (wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) { Toast.makeText(context, "WIFI已连接,移动数据已断开", Toast.LENGTH_SHORT).show(); } else if (!wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) { Toast.makeText(context, "WIFI已断开,移动数据已连接", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "WIFI已断开,移动数据已断开", Toast.LENGTH_SHORT).show(); }//API大于23时使用下面的方式进行网络监听 }else { System.out.println("API level 大于23"); //获得ConnectivityManager对象 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //获取所有网络连接的信息 Network[] networks = connMgr.getAllNetworks(); //用于存放网络连接信息 StringBuilder sb = new StringBuilder(); //通过循环将网络信息逐个取出来 for (int i=0; i < networks.length; i++){ //获取ConnectivityManager对象对应的NetworkInfo对象 NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]); sb.append(networkInfo.getTypeName() + " connect is " + networkInfo.isConnected()); } Toast.makeText(context, sb.toString(),Toast.LENGTH_SHORT).show(); } } }; @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton= (Button) findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //startService(new Intent(MainActivity.this,MyService.class)); mHandle.sendEmptyMessageDelayed(100,6000); } }); mHandle = new Handler() { @Override public void handleMessage(Message msg) { Intent intent=new Intent(); intent.setAction("test"); sendBroadcast(intent); super.handleMessage(msg); } }; checkDrawOverlayPermission();// mHandle.postDelayed(new Thread() {// @Override// public void run() {// Log.e("wxf", "111");// AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);//// dialog.setTitle("测试");// dialog.setIcon(android.R.drawable.ic_dialog_info);// dialog.setMessage("测试 ");// AlertDialog mDialog = dialog.create();// mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);// mDialog.show();// super.run();// }// }, 3000); } @RequiresApi(api = Build.VERSION_CODES.M) public void checkDrawOverlayPermission() { // Settings.canDrawOverlays() if (!Settings.canDrawOverlays(MainActivity.this)) { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, 111); } } @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 111) { if (Settings.canDrawOverlays(this)) { } } }}
public class reciber extends BroadcastReceiver{ @Override public void onReceive(final Context context, Intent intent) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("提示"); builder.setMessage("确定打开主界面吗?"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent1 = new Intent(context, MainActivity.class); //在广播接收器中启动活动,一定要给Intent加入FLAG_ACTIVITY_NEW_TASK标志 intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent1); } }); AlertDialog dialog = builder.create(); //需要把对话框的类型设为TYPE_SYSTEM_ALERT,否则对话框无法在广播接收器里弹出 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); dialog.show();////// AlertDialog.Builder dialog = new AlertDialog.Builder(context);//// dialog.setTitle("测试");// dialog.setIcon(android.R.drawable.ic_dialog_info);// dialog.setMessage("测试 ");// AlertDialog mDialog = dialog.create();// mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);// mDialog.show(); }}
0 0
- android广播中实现弹出窗口
- android弹出窗口实现
- android弹出窗口的实现
- android中使用PopupWindow实现弹出窗口菜单
- Android 中广播实现机制
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)!!!!
- Android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android pop自定义弹出窗口实现
- android弹出窗口的实现(PopupWindow)
- Android Demo---实现从底部弹出窗口
- android弹出窗口的实现(PopupWindow)
- Java多线程之Synchronized应用在static和非static方法上的区别
- MUI框架学习之[事件取消][事件触发][手势事件][自定义事件]
- jdbcTemplate的queryForList
- xhprof php7问题
- String(字符串)数目统计
- android广播中实现弹出窗口
- Java源代码阅读——HashMap实现原理
- Java 8 有用的新特性
- android studio 发布app出错:please select the product flavors to build and sign
- POJ1222
- 浅析NopCommerce的多语言方案
- Android Studio插件整理(三)
- 【C++学习笔记】Union关键字
- 模糊查询位置跳转