1. 安卓broadcastreceiver怎麼接收的廣播
1.廣播接收者(BroadcastReceiver)
廣播接收者(BroadcastReceiver)繼承BroadcastReceiver類接收廣播意圖的Java類,重寫:
public void onReceive(Context context,Intent intent),其中intent可以獲得傳遞的數據;
廣播意圖就是通過Context.sendBroadcast(Intent intent)或Context.sendOrderedBroadcast(Intentintent)發送的意圖,通過這個語句,能夠廣播給所有滿足條件的組件,比如intent設置了action="Receiver",則所有在androidManifest.xml中設置過<actionandroid:name="Receiver"/>的廣播接收者都能夠接收到廣播;
2.廣播發送者sendBroadcast()
通常廣播發送方就是調用Context.sendBroadcast()的程序,通常廣播發送方都是通過隱式意圖發送出去;
廣播發送方分為普通廣播和有序廣播;
同步廣播:發送方發出後,幾乎同時到達多個廣播接收者處,某個接收者不能接收到廣播後進行一番處理後傳給下一個接收者,並且無法終止廣播繼續傳播;Context.sendBroadcast(intent);
有序廣播:廣播接收者需要提前設置優先順序,優先順序高的先接收到廣播,優先順序數值為-1000~1000,在AndroidManifest.xml的<intent-filterandroid:priority="1">設置;比如存在3個廣播接收者A、B、C、D,優先順序A>B>C>D,因此A最先收到廣播,當A收到廣播後,可以向廣播中添加一些數據給下一個接收者(intent.putExtra()),或者終止廣播 abortBroadcast();Context.sendOrderedBroadcast(intent);
一、同步廣播發送方核心代碼
Intent intent = new Intent();
intent.setAction("Receiver");
Context.sendBroadcast(intent);
有序廣播發送方核心代碼:
Intent intent = new Intent();
intent.setAction("Receiver");
Context.sendOrderedBroadcast(intent,null);
二、廣播接收者核心代碼:
public class MyReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();...
}
}
三 注冊該廣播
AndroidManifest.xml 注冊方式
<receiver android:name=".MyReceiver">
<intent-filter android:priority="1000">
<action android:name="Receiver"/>
</intent-filter>
</receiver>
Java類注冊方式
publicvoid registerBoradcastReceiver()
{
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("Receiver");
registerReceiver(廣播類對象, myIntentFilter);
}
簡單例子
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Test extends Activity{
private final String ACTION_NAME = "Receiver";
private Button mBtnEvent = null;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//注冊廣播
registerBoradcastReceiver();
LinearLayout mLinearLayout = new LinearLayout(this);
mBtnEvent= new Button(this);
mBtnEvent.setText("發送廣播");
mLinearLayout.addView(mBtnMsgEvent);
setContentView(mLinearLayout);
mBtnEvent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendTestBroadcast();
}
});
}
//發送廣播
private void sendTestBroadcast()
{
Intent mIntent = new Intent(ACTION_NAME);
mIntent.putExtra("strData", "發送廣播,在這里傳送數據");
sendBroadcast(mIntent);
}
private BroadcastReceiver myReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(ACTION_NAME)){
Toast.makeText(Test.this, "接收測試", 200);
}
}
};
//注冊廣播
public void registerBoradcastReceiver(){
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(ACTION_NAME);
registerReceiver(myReceiver, myIntentFilter);
}
}
2. 怎麼接收android 靜態廣播
在AndroidManifest.xml注冊的方式,Android不能自動銷毀廣播接收器,也就是說當應用程序關閉後,廣播接收器還是會接收廣播,而在代碼中注冊廣播接收器,可以讓程序員手動定義銷毀接收器的代碼。應該根據應用程序的需求來選擇實現廣播機制的方法。
3. android廣播有多個接收器
_<intent-filter>就可以了,
例:以下的MyBroadcastReceiver可以同時監聽farsight.inf1和farsight.inf2類型的廣播
<application Android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="com.fs.receiver.broadcast.MyBroadcastReceiver">
<intent-filter>
<action android:name="farsight.inf1" />
</intent-filter>
<intent-filter>
<action android:name="farsight.inf2" />
</intent-filter>
</receiver>
</application>
當廣播監聽到廣播後監聽器類中函數public void onReceive(Context context, Intent intent) 會被調用,
為了區別到底監聽到的是farsight.inf1類型的廣播還是farsight.inf2類型的廣播,可以使用函數中參數intent調
用
getAction()函數,該函數的返回值會返回farsight.inf1或farsight.inf2.用返回的表達類型的字元串則可以
知道是監聽到哪一個廣播被監聽到後調用的onReceive函數
3. 多個廣播接收器可以接收同一廣播比較簡單,只要創建多個廣播接收器類,然後在MyBroadcastReceiver.xml
中將這些廣播接收器都配置好!並且配置相同的<intent-filter>就可以了
例:
廣播接收器一的代碼
public class OneBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
System.out.printfln("One....");
}
}
廣播接收器二的代碼
public class TwoBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
System.out.printfln("Two....");
}
}
如果以上兩監聽器類在同一個項目中,則在AndroidManifest.xml的<application>標簽中嵌套配置
兩個<receiver>標簽
<receiver android:name="OneBroadcastReceiver類的包名.OneBroadcastReceiver">
<intent-filter>
<action android:name="farsight.inf" />
</intent-filter>
</receiver>
<receiver android:name="TwoBroadcastReceiver類的包名.TwoBroadcastReceiver">
<intent-filter>
<action android:name="farsight.inf" />
</intent-filter>
</receiver>
如果以兩個接收器不是在同一個項目中,則分別把上面的兩組<receiver>標簽配置在分自所在的
項目的 AndroidManifest.xml文件的<application>標簽中配置即可,
以上做完後,只要發送廣播的類型為farsight.inf,則OneBroadcastReceiver和TwoBroadcastReceiver類中的onReceive函數都會被自動調用,
當多個廣播接收器監聽同一個廣播,還要控制其中接收器接收順序,那就會用到另一個知識點——有序廣播
4. Android中怎麼打開Wifi的組播功能
打開Android中Wifi的組播功能。主要有以下幾個步驟:
在Manifest文件中加入:android.permission.CHANGE_WIFI_MULTICAST_STATE,這個許可權
獲取到MulticastLock對象,這個對象不能直接實例化,要通過WifiManager間接得到,工廠模式
調用MulticastLock對象的acquire方法,獲取到組播鎖
相應的,用完組播,為了不浪費電力,要調用MulticastLock的release方法釋放鎖
下面寫了個簡單示例,通過組播發現伺服器。
Activity寫的比較簡單
public class MulticastDemoActivity extends Activity {
MulticastLock multicastLock;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
allowMulticast();
try {
NetUtil.findServerIpAddress();
} catch (IOException e) {
throw new RuntimeException(e);
}
Log.d("multicast.demo", "find ip ok.");
multicastLock.release();
}
private void allowMulticast(){
WifiManager wifiManager=(WifiManager)getSystemService(Context.WIFI_SERVICE);
multicastLock=wifiManager.createMulticastLock("multicast.test");
multicastLock.acquire();
}
}
在Activity中打開和釋放組播鎖。使用組播發送報文和接收ip地址信息的工具類代碼:
public class NetUtil {
private static final String TAG="Net.Utils";
private static final int MULTICAST_PORT=5111;
private static final String GROUP_IP="224.5.0.7";
private static byte[] sendData;
static{
sendData=new byte[4];
// 0xEE78F1FB
sendData[3] = (byte) 0xEE;
sendData[2] = (byte) 0×78;
sendData[1] = (byte) 0xF1;
sendData[0] = (byte) 0xFB;
}
public static String findServerIpAddress() throws IOException{
String ip=null;
MulticastSocket multicastSocket=new MulticastSocket(MULTICAST_PORT);
multicastSocket.setLoopbackMode(true);
InetAddress group = InetAddress.getByName(GROUP_IP);
multicastSocket.joinGroup(group);
DatagramPacket packet=new DatagramPacket(sendData, sendData.length,group,MULTICAST_PORT);
for(;;){
multicastSocket.send(packet);
Log.d(TAG,">>>send packet ok");
byte[] receiveData=new byte[256];
packet=new DatagramPacket(receiveData, receiveData.length);
multicastSocket.receive(packet);
String packetIpAddress=packet.getAddress().toString();
packetIpAddress=packetIpAddress.substring(1, packetIpAddress.length());
Log.d(TAG,"packet ip address: "+packetIpAddress);
StringBuilder packetContent=new StringBuilder();
for(int i=0;i<receiveData.length;i++){
if(receiveData[i]==0){
break;
}
packetContent.append((char)receiveData[i]);
}
ip=packetContent.toString();
Log.d(TAG,"packet content ip is: "+ip);
if(ip.equals(packetIpAddress)){
Log.d(TAG,"find server ip address: "+ip);
break;
}else{
Log.d(TAG,"not find server ip address, continue …");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
return ip;
}
}
5. 如何用android實現廣播,硬體接收到廣播可以連接上路由器
你好!這可不是一兩句能說明白的!首先你要會安卓系統的編程;再有,就是要會單片機的編程;最後,要有相應的硬體支持,才能完成調試
6. android廣播接收的onReceive方法不執行,即接收不到廣播的消息
最大的可能是廣播沒有注冊
1)第一種不是常駐型廣播,也就是說廣播跟隨activity的生命周期。注意: 在activity結束前,移除廣播接收器。(代碼里注冊)
2)第二種是常駐型,也就是說當應用程序關閉後,如果有信息廣播來,程序也會被系統調用自動運行。(androidmanifest.xml注冊)
7. 安卓廣播接收器怎麼判斷是哪個應用發送的想要只接收自身應用廣播,怎麼實現
安卓廣播接收器Intent,它有提供過濾器功能,也就是開發者定義過濾條件,只有是這個條件的廣播才接收,這樣就可以接收指定廣播。這也是安卓廣播的基礎知識功能,具體自己查閱相關資料,很簡單的。
8. android-Android udp接收不到組播,請問如何解決
這是因為在AP模式下,沒有添加組播路由。你可以在AP模式下的時候,去查看一下wifi的信息,你會發現除了默認的ip之外,wifi上沒有相應的組播路由
9. android wifi 組播
224.0.0.0~224.0.0.255為預留的組播地址(永久組地址),地址224.0.0.0保留不做分配,其它地址供路由協議使用;
224.0.1.0~224.0.1.255是公用組播地址,可以用於Internet;
224.0.2.0~238.255.255.255為用戶可用的組播地址(臨時組地址),全網范圍內有效
239.0.0.0~239.255.255.255為本地管理組播地址,僅在特定的本地范圍內有效。
把地址改到239.0.0.0~239.255.255.255之間。看看