动/静态接收系统广播
来源:互联网 发布:ubuntu 查看mysql 编辑:程序博客网 时间:2024/06/06 03:38
动态接收系统广播:
先创建IntentFilter作为广播内容,再写一个继承于BroadcastReceiver的类作为接收器,然后注册接收器,最后在AndroidManifest.xml添加相关许可。代码如下:
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.Toast;public class Broadcast extends AppCompatActivity { NetworkChangeReceiver networkChangeReceiver; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter intentFilter=new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); //设置为网络变化活动 networkChangeReceiver=new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver,intentFilter); //注册广播接收器,第一个参数为BroadcastReceiver类,第二个参数为广播内容 } @Override protected void onDestroy(){ super.onDestroy(); unregisterReceiver(networkChangeReceiver); }}class NetworkChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent){ Toast.makeText(context,"网络发生了改变!",Toast.LENGTH_SHORT).show(); }}
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
静态注册实现开机启动:
在一个独立的类中继承BroadcastReceiver写覆盖方法执行收到广播后的操作,然后在AndroidManifest中写<receiver和<use-permission
Broadcast.class
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class Broadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent){ Toast.makeText(context,"Boot Completed!",Toast.LENGTH_SHORT).show(); }}AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mole.test"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <use-permission android:name="android.permission.RECEIVER_BOOT_COMPLETED"/> <receiver android:name=".Broadcast" android:enabel="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
阅读全文
0 0
- 动/静态接收系统广播
- 【Android成长之路】全局大喇叭——广播机制的浅谈(接收系统广播与动、静态注册接收器)
- 动静态广播的区别
- 广播接收器动静态注册
- Android中的静态系统广播与动态系统广播及其相应的注册与接收
- android-接收系统广播
- 接收系统广播
- 接收系统广播
- android接收系统广播
- BroadcastReceiver 接收系统短信广播
- Android 接收系统发送的广播,动态注册和静态注册
- android关于静态广播无法接收2
- Android 静态广播和动态广播接收顺序
- 动态广播和静态广播接收时间备注
- 杀掉进程,静态广播接收器不再能接收到广播
- 静态注册的广播每次接收广播都会重新生成一个接收广播的对象
- 系统广播详解之接收短信
- Android开发之接收系统广播消息
- Redis:存储对象的两种方式(序列化和json字符串)
- 用两个队列实现栈和用两个栈实现队列
- 51nod 1267 4个数和为0
- public class Solution { public int countPrimes(int n) { boolean[] isDelArray = new boolean[n];
- PowerDesigner中NAME和COMMENT的互相转换,需要执行语句
- 动/静态接收系统广播
- Mysql 表设计
- Socket的简单数据传递
- CreateProcessAsUser常用参数说明
- Dinic最大流(bzoj 2756: [SCOI2012]奇怪的游戏)
- opencv学习之数据结构与数据操作
- python Sublime Text3的使用基本配置操作&&SublimeREPL插件的安装及快捷键设置
- JAVA 攻城狮 第三十二天
- Java格式化倒计时的两种方式