创建一个类继承自BroadcastReceiver
public class Notication extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("haha");
System.out.println("收到广播"+getResultData()+"===="+intent.getAction());
}
}
设置receiver
<receiver android:name="com.example.broadcastreceiver.Notication">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
<!-- 注意!! 这句必须要加,否则接收不到BroadCast -->
</intent-filter>
<intent-filter >
<action android:name="sbsb"/>
</intent-filter>
</receiver>
配置对应的权限
<uses-permission android:name="android.permission.RESTART_PACKAGES"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
简单的发送一个无序广播
public void click(View v){
Intent it = new Intent();
it.setAction("sbsb");
// it.setData(Uri.parse("XIAOER"));
sendBroadcast(it);
}