1. 程式人生 > >Android中廣播的使用程式碼,通俗易懂介紹廣播獲取使用(BroadcastReceiver)

Android中廣播的使用程式碼,通俗易懂介紹廣播獲取使用(BroadcastReceiver)

/***********************佈局檔案***********************/

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zhangyanbo.day21_broadcastdynamic.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="註冊廣播"
        android:onClick="registerBoradcastReceiver"
        android:id="@+id/button" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消註冊廣播"
        android:onClick="unregisterBoradcastReceiver"
        android:layout_below="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/button2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="傳送廣播"
        android:onClick="sendBoradcast"
        android:layout_below="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

/***********************主程式,實現廣播***********************/

public class MainActivity extends AppCompatActivity {
    //接收廣播的意圖過濾器
    private IntentFilter intentFilter = new IntentFilter();

    //廣播接收器
    private DynamicBroadcastReceiver receiver = new DynamicBroadcastReceiver() ;

    //廣播的註冊狀態
    private boolean isReceiverRegistered;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //註冊廣播接收器
    public void registerBoradcastReceiver(View view){

        //如果廣播接收器未註冊,則允許註冊
        if(!isReceiverRegistered){
            //意圖過濾器中新增action過濾條件
            intentFilter.addAction("com.dynamic.broadcast");
            //註冊廣播接收器
            this.registerReceiver(receiver,intentFilter);
            //廣播註冊完畢,設定註冊狀態為true;
            isReceiverRegistered = true;
        }
    }

    //取消註冊廣播接收器
    public void unregisterBoradcastReceiver(View view){
        //如果廣播接收器已註冊,則允許取消
        if(isReceiverRegistered){
            unregisterReceiver(receiver);
            isReceiverRegistered = false;
        }

    }


    //傳送廣播
    public void sendBoradcast(View view){
        Intent intent = new Intent();

        intent.setAction("com.dynamic.broadcast");
        intent.putExtra("msg", "你是**嗎?再給我卡試試");
        sendBroadcast(intent);
        Toast.makeText(this,"傳送成功",Toast.LENGTH_SHORT).show();
    }

}

/***********************BroadcastReceiver***********************/

public class DynamicBroadcastReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("msg");
        Log.d("zfr", "接收端:" + msg);
    }
}