广播接收者
BroadcastReceiver
方法的使用:
一、系统的广播接收者
- 创建一个BroadcastReceiver类;
- 重写onService()方法;
-
在配置文件中注册,
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission> <receiver /*name:创建类所在的全路径名;*/ android:name="com.example.com.my_broadcast.BroadcasRe ceiver1"> <intent-filter> /*name:需要给所监听的事件 添加权限*/ <action android:name="android.intent.action.NEW_OUTGOING_CALL"></action> </intent-filter> </receiver>
二、自定义广播接收者
- 新建一个BroadcastReceiver类
-
在MainActivity中写入:
Intent intent = new Intent(); intent.setaction("com.broadcast.cn") sendBroadcast(intent);
-
在配置文件中注册:
<receiver android:name=".Broadcast"> <intent-filter> <action android:name="com.BroadCast.con"/> </intent-filter> </receiver>
或者在MainActivit.java中写入:
broad = new Broadcast(); String action = "com.BroadCast.con"; IntentFilter filter = new IntentFilter(action); registerReceiver(broad, filter);