android廣播傳遞
❶ android 怎樣將message對象通過廣播傳輸
線程中:
Message msg = Message.obtain();
msg.obj = result1; //從這里把你想傳遞的數據放進去就行了
handler.sendMessage(msg);
線程外:
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String getResult1=(String)msg.obj;
}
};
❷ 簡述在android中如何發送廣播消息
1.發送廣播
Intent intent = new Intent(BroadcastAction);
Bundle bundle = new Bundle();
bundle.putString("***", SUCCESS);
bundle.putString("FullPathName", mFullPathName);
intent.putExtras(bundle);
sendBroadcast(intent);
2.在Activity中創建一個內部類MyBroadcastReceiver擴展BroadcastReceiver,並在其中實現onReceive方法。
3.在Activity中聲明一個MyBroadcastReceiver類型的成員變數,並注冊:
private MyBroadcastReceiver myBroadcastReceiver;
...
myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(BroadcastAction);
registerReceiver(receiver, filter);
4.使用完後要記得釋放
unregisterReceiver(receiver);
註:1和2中的 BroadcastAction要是同一個Action
❸ android怎麼發送特定廣播的
起一個線程,每發一個廣播後就sleep一分鍾,如此循環。(或者接受系統的timechanged這個廣播,這個廣播好像一分鍾發一次)。
Android 在發送廣播時的方法 sendBroadcast(Intent)。
①:Intent myIntent = new Intent();——【創建Intent對象】
②:myIntent.setAction(String)——【設置一般的要執行的動作。參數:動作一個動作的名稱,如ACTION_VIEW。應用程序的具體行動,應與供應商的包名作為前綴。】
③:myIntent.putExtra(String,Object)——【廣播中額外發送的數據,String為自定義key,Object表示多種數據類型】
④:sendBroadcast(myIntent);——【發送廣播】
接收廣播
Android在接收廣播的方法是注冊一個廣播接收器 registerReceiver(MyReceiver,IntentFilter)。
①:首先創建MyReceiver類(類名自定義) 繼承 BroadcastReceiver類。——【創建廣播接收器】
②:在MyReceiver中重寫public void onReceive(Context context, Intent intent)方法。這個方法在接收到廣播後觸發。——【重寫處理方法】
③:在Activity或者Service啟動時 onCreate()、onStartCommand()等方法中實例化 MyReceiver類——【啟動時實例化廣播接收器】
④:IntentFilter filter = new IntentFilter();——【創建IntentFilter對象 意圖過濾器】
⑤:filter.addAction(String);——【在過濾器中加入過濾條件,說明接收什麼廣播】
⑥:registerReceiver(cmdReceiver, filter);——【注冊廣播,參數為(廣播接收器,意圖過濾器)】
❹ android-Android廣播怎麼傳遞數據給Activity
一種是像樓主所說的,在Activity里注冊一個BroadcastReceiver,Service完成某個任務就可以發一個廣播,接收器收到廣播後通知activity做相應的操作。 另一種是,使用bindService來關聯Service和Application,應用.apk里的所有組件一般情況都運行在同一個進程中,所以不需要用到IPC,bindService成功後,Service的Client可以得到Service返回的一個iBinder引用,具體的參見Service的文檔及onBind的例子,這樣Service的引用就可以通過返回的iBinder對象得到,如 public class LocalService extends Service { // This is the object that receives interactions from clients. See // RemoteService for a more complete example. private final IBinder mBinder = new LocalBinder(); public class LocalBinder extends Binder { LocalService getService() { return LocalService.this; } } @Override public IBinder onBind(Intent intent) { return mBinder; } } 之後Client通過這個iBinder對象得到Service對象引用之後,可以直接和Service通訊,比如讀取Service中的值或是調用Service的方法。
❺ 注冊廣播有幾種方式,這些方式有何優缺點請談談Android引入廣播機制的用意。
android中,不同進程之間傳遞信息要用到廣播,可以有兩種方式來實現。
第一種方式:在Manifest.xml中注冊廣播,是一種比較推薦的方法,因為它不需要手動注銷廣播(如果廣播未注銷,程序退出時可能會出錯)。
具體實現在Manifest的application中添加:上面兩個android:name分別是廣播名和廣播的動作(這里的動作是表示系統啟動完成),如果要自己發送一個廣播,在代碼中為:
Intent i = new Intent(「android.intent.action.BOOT_COMPLETED」);
sendBroadcast(i);
這樣,廣播就發出去了,然後是接收。
接收可以新建一個類,繼承至BroadcastReceiver,也可以建一個BroadcastReceiver的實例,然後得寫onReceive方法,實現如下:
protected BroadcastReceiver mEvtReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(「android.intent.action.BOOT_COMPLETED」)) {
//Do something
}
}
};
第二種方式,直接在代碼中實現,但需要手動注冊注銷,實現如下:
IntentFilter filter = new IntentFilter();
filter.addAction(「android.intent.action.BOOT_COMPLETED」);
registerReceiver(mEvtReceiver, filter); //這時注冊了一個recevier ,名為mEvtReceiver,然後同樣用上面的方法以重寫onReceiver,
最後在程序的onDestroy中要注銷廣播,實現如下:
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mPlayerEvtReceiver);
}
❻ android 廣播傳值
intent.putExtras(intent); 這條語句不是很矛盾嗎? 去掉這條語句就可以了
startService(intent);這個時候已經把intent傳遞給service了
你看一下service里的onStart 方法,不是傳遞了一個intent嗎
請採納哈.
❼ android 怎麼發送系統廣播
分為4步:
首先要聲明廣播
其次要注冊廣播,有兩種方式:xml注冊和代碼注冊
發送廣播
收聽開機廣播
268101305698999
❽ Android開發中怎麼把廣播中處理後的信息傳遞給Activity
1.發送廣播
Intent intent = new Intent(BroadcastAction);
Bundle bundle = new Bundle();
bundle.putString("***", SUCCESS);
bundle.putString("FullPathName", mFullPathName);
intent.putExtras(bundle);
sendBroadcast(intent);
2.在Activity中創建一個內部類MyBroadcastReceiver擴展BroadcastReceiver,並在其中實現onReceive方法。
3.在Activity中聲明一個MyBroadcastReceiver類型的成員變數,並注冊:
private MyBroadcastReceiver myBroadcastReceiver;
...
myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(BroadcastAction);
registerReceiver(receiver, filter);
4.使用完後要記得釋放
unregisterReceiver(receiver);
註:1和2中的 BroadcastAction要是同一個Action
❾ android中廣播可以實現進程間通信嗎
只有你允許客戶端從不同的應用程序為了進程間的通信而去訪問你的service,以及想在你的service處理多線程,下面為大家詳細介紹下
Android Service是分為兩種:
本地服務(Local Service): 同一個apk內被調用
遠程服務(Remote Service):被另一個apk調用
遠程服務需要藉助AIDL來完成。
AIDL 是什麼
AIDL (Android Interface Definition Language) 是一種IDL 語言,用於生成可以在Android設備上兩個進程之間進行進程間通信(interprocess communication, IPC)的代碼。如果在一個進程中(例如Activity)要調用另一個進程中(例如Service)對象的操作,就可以使用AIDL生成可序列化的參數。
AIDL IPC機制是面向介面的,像COM或Corba一樣,但是更加輕量級。它是使用代理類在客戶端和實現端傳遞數據。
AIDL 的作用
由於每個應用程序都運行在自己的進程空間,並且可以從應用程序UI運行另一個服務進程,而且經常會在不同的進程間傳遞對象。在Android平台,一個進程通常不能訪問另一個進程的內存空間,所以要想對話,需要將對象分解成操作系統可以理解的基本單元,並且有序的通過進程邊界。
通過代碼來實現這個數據傳輸過程是冗長乏味的,Android提供了AIDL工具來處理這項工作。
選擇AIDL的使用場合
官方文檔特別提醒我們何時使用AIDL是必要的:只有你允許客戶端從不同的應用程序為了進程間的通信而去訪問你的service,以及想在你的service處理多線程。
如果不需要進行不同應用程序間的並發通信(IPC),you should create your interface by implementing a Binder;或者你想進行IPC,但不需要處理多線程的,則implement your interface using a Messenger。無論如何,在使用AIDL前,必須要理解如何綁定service——bindService。
❿ Android啟動廣播時怎樣往廣播中傳遞參數
我們在android中使用廣播來讓其他監聽廣播的地方能夠對相應的事情做處理,但有的時候我們仍然需要傳遞一些其他的附帶值,而這個時候是可以直接用播放廣播的intent來傳遞的。
例:
Intent intent = new Intent();
intent.putExtra("msgPersons", msgPersons);
intent.setAction(Constant.hasMsgUpdatedAction);
intent.putExtra("userId", userId);
intent.putExtra("msgCount", messages.size());
sendBroadcast(intent);
監聽廣播的代碼:
if (type.equals(Constant.hasMsgUpdatedAction)) {
if (obj instanceof Intent) {
Intent intent = (Intent) obj;
String msgPersons = intent.getStringExtra("msgPersons");
.......
}
}
這里的obj實際上是廣播監聽函數public void onReceive(String type, Object obj)中的第二個參數。當時看到這個函數的時候,一直不明白第二個參數的作用,後來才發現,原來還可以通過它來得到intent。