簡單使用
步驟:
- 傳送一條廣播
Intent intent=new Intent("sunweihao");
sendBroadcast(intent);
- 接收廣播:
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String ACTION1="sunweihao";
private static final String ACTION2="sunweihao2";
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION1==intent.getAction()) {
Toast.makeText(context, "孫偉豪", Toast.LENGTH_SHORT).show();
}else if (ACTION2==intent.getAction()) {
Toast.makeText(context, "孫偉豪2", Toast.LENGTH_SHORT).show();
}
}
}
- 註冊廣播
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="sunweihao"/>
<action android:name="sunweihao2"/>
</intent-filter>
</receiver>