广播接收者
一、廣播發送者&廣播接收者介紹
1.廣播接收者
? ? 廣播接收者簡單地說就是接收廣播意圖的Java類,此Java類繼承BroadcastReceiver類,重寫:
? ? public void onReceive(Context context,Intent intent),其中intent可以獲得傳遞的數據;
? ? 廣播意圖就是通過Context.sendBroadcast(Intent intent)或Context.sendOrderedBroadcast(Intent intent)發送的意圖,通過這個語句,能夠廣播給所有滿足條件的組件,比如intent設置了action="com.xiazdong",則所有在AndroidManifest.xml中設置過<action android:name="com.xiazdong"/>的廣播接收者都能夠接收到廣播;
? ??
? ? 注:onReceive方法必須在10秒內完成,如果沒有完成,則拋“Application No Response”當廣播接收者onReceive方法需要執行很長時間時,最好將此耗時工作通過Intent發送給Service,由Service完成,并且不能使用子線程解決,因為BroadcastReceiver是接收到廣播后才創建的,并且生命周期很短,因此子線程可能在沒有執行完就已經被殺死了。
? ??
? ??
? ? public void onReceive(Context context,Intent intent){ ?
? ? ? ? Intent intent = new Intent(context,XxxService.class); ?
? ? ? ? context.startService(intent); ?
? ? } ?
2.廣播發送者
? ? 通常廣播發送方就是調用Context.sendBroadcast()的程序,而廣播接收者就是繼承BroadcastReceiver的程序;
? ? 通常廣播發送方都是通過隱式意圖,這樣才能發送給多人;
? ??
? ? 廣播發送方分為普通廣播和有序廣播;
? ? 普通廣播:發送方發出后,幾乎同時到達多個廣播接收者處,某個接收者不能接收到廣播后進行一番處理后傳給下一個接收者,并且無法終止廣播繼續傳播;Context.sendBroadcast(intent);
? ? 有序廣播:廣播接收者需要提前設置優先級,優先級高的先接收到廣播,優先級數值為-1000~1000,在AndroidManifest.xml的<intent-filter android:priority="xxx">設置;比如存在3個廣播接收者A、B、C,優先級A>B>C,因此A最先收到廣播,當A收到廣播后,可以向廣播中添加一些數據給下一個接收者(intent.putExtra()),或者終止廣播(abortBroadcast());
? ? Context.sendOrderedBroadcast(intent);
二、廣播接收者核心代碼
? ? 同步廣播發送方核心代碼:
? ? ? ? Intent intent = new Intent(); ?
? ? ? ? intent.setAction("..."); ?
? ? ? ? Context.sendBroadcast(intent); ?
? ??
? ??
? ? 有序廣播發送方核心代碼:
? ? ? ? Intent intent = new Intent(); ?
? ? ? ? intent.setAction("..."); ?
? ? ? ? Context.sendOrderedBroadcast(intent,null); ?
? ??
? ??
? ? 廣播接收者核心代碼:
? ??
? ? ? ? public class Receiver extends BroadcastReceiver{ ?
? ? ? ? ? ? public void onReceive(Context context, Intent intent) { ?
? ? ? ? ? ? ? ? Bundle bundle = intent.getExtras(); ?
? ? ? ? ? ? ? ? ... ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? AndroidManifest.xml
? ? <application> ? ? ? ? ??
? ? ? ? <receiver android:name=".Receiver"> ??
? ? ? ? ? ? <intent-filter android:priority="1000"> ??
? ? ? ? ? ? ? ? <action android:name="com.xiazdong"/> ?
? ? ? ? ? ? </intent-filter> ?
? ? ? ? </receiver> ?
? ? </application> ? ? ? ? ?
三、廣播實例
1.同步廣播實例
? ? (1)廣播發送者:
? ??
? ? package com.xiazdong.broadcastsender; ?
? ??
? ? import android.app.Activity; ?
? ? import android.content.Intent; ?
? ? import android.os.Bundle; ?
? ? import android.view.View; ?
? ? import android.view.View.OnClickListener; ?
? ? import android.widget.Button; ?
? ? import android.widget.Toast; ?
?
? ? public class MainActivity extends Activity { ?
? ? ? ? private Button button; ?
? ? ? ? @Override ?
? ? ? ? public void onCreate(Bundle savedInstanceState) { ?
? ? ? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? ? ? setContentView(R.layout.main); ?
? ? ? ? ? ? button = (Button)this.findViewById(R.id.button); ?
? ? ? ? ? ? button.setOnClickListener(listener); ?
? ? ? ? } ?
? ? ? ? private OnClickListener listener = new OnClickListener(){ ?
? ? ? ? ? ? @Override ?
? ? ? ? ? ? public void onClick(View v) { ?
? ? ? ? ? ? ? ? Intent intent = new Intent(); ?
? ? ? ? ? ? ? ? intent.setAction("com.xiazdong"); ?
? ? ? ? ? ? ? ? intent.putExtra("name", "xiazdong"); ?
? ? ? ? ? ? ? ? MainActivity.this.sendBroadcast(intent); ?
? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "發送廣播成功", Toast.LENGTH_SHORT).show(); ?
? ? ? ? ? ? } ?
? ? ? ? }; ?
? ? } ?
(2)廣播接收者
? ? package com.xiazdong.broadcastreceiver1; ?
? ??
? ? import android.content.BroadcastReceiver; ?
? ? import android.content.Context; ?
? ? import android.content.Intent; ?
? ? import android.util.Log; ?
? ??
? ? public class Receiver extends BroadcastReceiver { ?
? ??
? ? ? ? @Override ?
? ? ? ? public void onReceive(Context context, Intent intent) { ?
? ? ? ? ? ? String name = intent.getExtras().getString("name"); ?
? ? ? ? ? ? Log.i("Recevier1", "接收到:"+name); ?
? ? ? ? } ?
? ??
? ? } ?
? ? AndroidManifest.xml
? ??
? ? <receiver android:name=".Receiver"> ?
? ? ? ? <intent-filter> ?
? ? ? ? ? ? <action android:name="com.xiazdong"/> ?
? ? ? ? </intent-filter> ?
? ? </receiver> ?
2.有序廣播實例
? ? 場景說明:
? ??
? ??
? ? (1)廣播發送者
? ??
? ? package com.xiazdong.broadcastsender; ?
? ??
? ? import android.app.Activity; ?
? ? import android.content.Intent; ?
? ? import android.os.Bundle; ?
? ? import android.view.View; ?
? ? import android.view.View.OnClickListener; ?
? ? import android.widget.Button; ?
? ? import android.widget.Toast; ?
?
? ? public class MainActivity extends Activity { ?
? ? ? ? private Button button; ?
? ? ? ? @Override ?
? ? ? ? public void onCreate(Bundle savedInstanceState) { ?
? ? ? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? ? ? setContentView(R.layout.main); ?
? ? ? ? ? ? button = (Button)this.findViewById(R.id.button); ?
? ? ? ? ? ? button.setOnClickListener(listener); ?
? ? ? ? } ?
? ? ? ? private OnClickListener listener = new OnClickListener(){ ?
? ? ? ? ? ? @Override ?
? ? ? ? ? ? public void onClick(View v) { ?
? ? ? ? ? ? ? ? Intent intent = new Intent(); ?
? ? ? ? ? ? ? ? intent.setAction("com.xiazdong"); ?
? ? ? ? ? ? ? ? intent.putExtra("name", "xiazdong"); ?
? ? ? ? ? ? ? ? MainActivity.this.sendOrderedBroadcast(intent, null); ? //有序廣播發送 ?
? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "發送廣播成功", Toast.LENGTH_SHORT).show(); ?
? ? ? ? ? ? } ?
? ? ? ? }; ?
? ? } ?
? ? (2)廣播接收者
? ??
? ??
? ? Receiver1
? ? package com.xiazdong.broadcastreceiver1; ?
? ??
? ? import android.content.BroadcastReceiver; ?
? ? import android.content.Context; ?
? ? import android.content.Intent; ?
? ? import android.util.Log; ?
? ??
? ? public class Receiver extends BroadcastReceiver { ?
? ??
? ? ? ? @Override ?
? ? ? ? public void onReceive(Context context, Intent intent) { ?
? ? ? ? ? ? String name = intent.getExtras().getString("name"); ?
? ? ? ? ? ? Log.i("Recevier1", "接收到:"+name); ?
? ? ? ? ? ? abortBroadcast(); ? //Receiver1接收到廣播后中斷廣播 ?
? ? ? ? } ?
? ??
? ? } ?
? ? AndroidManifest.xml
? ??
? ? <receiver android:name=".Receiver"> ?
? ? ? ? <intent-filter android:priority="1000"> <!-- 設置最高優先級 --> ?
? ? ? ? ? ? <action android:name="com.xiazdong"/> ?
? ? ? ? </intent-filter> ?
? ? </receiver> ?
轉載于:https://blog.51cto.com/yym631/1744732
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: 对网站的代码采集实例
- 下一篇: 产品经理的工作