sendBroadcast 와 sendSticky Broadcast 의 차이

4971 단어 sendbroadcast
이들 이 평소에 가장 자주 사용 하 는 것 은 sendbBroadcast 로 인 텐트 를 방송 하 는 것 이다.오늘 와 이 파 이 를 보 던 중 sendSticky Broadcast 도 발견 했다.공식 문 서 는 이렇게 쓰 여 있 습 니 다:public abstract void sendSticky Broadcast(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 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.네가 예 를 써 야만 알 수 있다.
 
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);     }         }
 /*** @author 장 흥 업*메 일 박스:xy-zhang\#163.com*android 개발 진급 군:278401545*/

좋은 웹페이지 즐겨찾기