簡單使用
步驟:
- 發送一條廣播
//發送一條廣播
//動作
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/139850.html
標籤:其他
上一篇:撰寫Java程式,使用ThreadLocal類,專案中創建賬戶類 Account,類中包括賬戶名稱name、 ThreadLocal 類的參考變數amount,表示存款
下一篇:Python背景關系管理器
