日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

sendBroadcast和sendStickyBroadcast的区别

發(fā)布時(shí)間:2025/3/17 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sendBroadcast和sendStickyBroadcast的区别 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

們平時(shí)最經(jīng)常使用的是sendBroadcast,就是把一個(gè)Intent廣播出去。今天我在看wifi的時(shí)候,還發(fā)現(xiàn)了sendStickyBroadcast。官方文檔是這樣寫(xiě)的:?


public abstract void sendStickyBroadcast (Intent intent)?

Since: API Level 1?
Perform a sendBroadcast(Intent) that is "sticky," meaning the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent).?
You must hold the BROADCAST_STICKY? permission in order to use this API. If you do not hold that permission, SecurityException will be thrown.
Parameters
?

intent The Intent to broadcast; all receivers matching this Intent will receive the broadcast, and?the Intent will be held to be re-broadcast to future receivers.?

光從字面的意思是很難理解的。只有你寫(xiě)例子才會(huì)明白的。

?

package com.android.testbroadcast; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button btnSendi; Button btnSends; Button btnStart; Context mContext; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSendi=(Button) findViewById(R.id.sendi); btnSends=(Button) findViewById(R.id.sends); btnStart=(Button) findViewById(R.id.start); mContext=getApplicationContext(); btnSendi.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("com.android.my.action"); intent.setFlags(1); mContext.sendBroadcast(intent); } }); btnStart.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,ReceiverActivity.class); startActivity(intent); } }); btnSends.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("com.android.my.action.sticky"); intent.setFlags(2); mContext.sendStickyBroadcast(intent); } }); } }


?

package com.android.testbroadcast;
?

import android.app.Activity;
?
import android.content.BroadcastReceiver;
?
import android.content.Context;
?
import android.content.Intent;
?
import android.content.IntentFilter;
?
import android.net.wifi.WifiManager;
?
import android.os.Bundle;
?
import android.view.View;
?
import android.view.View.OnClickListener;
?
import android.widget.Button;
?

public class ReceiverActivity extends Activity {
?
???????? private IntentFilter mIntentFilter;
?
????????
??? /** Called when the activity is first created. */
?
??? @Override
?
??? public void onCreate(Bundle savedInstanceState) {
?
??????? super.onCreate(savedInstanceState);
?
??????? setContentView(R.layout.main);
?
??????? mIntentFilter = new IntentFilter();
?
??????? mIntentFilter.addAction("com.android.my.action");
?
??????? mIntentFilter.addAction("com.android.my.action.sticky");
?

????????????????????????
??? }
?
??? private BroadcastReceiver mReceiver = new BroadcastReceiver() {
?

??????? @Override
?
??????? public void onReceive(Context context, Intent intent) {
?
??????????? final String action = intent.getAction();
?
??????????? System.out.println("action"+action);
?
????????????
?
??????? }
?
??? };
?
????
??? @Override
?
??? protected void onResume() {
?
??????????? // TODO Auto-generated method stub
?
??????????? super.onResume();
?
??????????? registerReceiver(mReceiver, mIntentFilter);
?
??? }
?
????
??? @Override
?
??? protected void onPause() {
?
??????????? // TODO Auto-generated method stub
?
??????????? super.onPause();
?
??????????? unregisterReceiver(mReceiver);
?
??? }
?
????
????
}



? ? ?本文轉(zhuǎn)自xyz_lmn51CTO博客,原文鏈接:http://blog.51cto.com/xyzlmn/1230800,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者


總結(jié)

以上是生活随笔為你收集整理的sendBroadcast和sendStickyBroadcast的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。