2011年12月29日 星期四

Android - sendStickyBroadcast(1) - 簡介

sendBroadcast()的用法大家比較常用,這邊不介紹。我們介紹sendStickyBroadcast()的用法,請先看官方說明。基本上是說,sticky intent在被broadcast出去之後,還會留在系統內,不會消失。如果有某個component有register這個Intent可以被receive的話,當這個component被執行時,還是可以receive這個「早就broadcast過的Intent」。sendStickyBroadcast()讓有component去register一個receiver收這個Intent時,馬上就可以收到。

也就是說,如果是sendBroadcast()只能實現,先執行component,再broadcast Intent,component才能收到這個Intent。但是改用 sendStickyBroadcast()則可以實現先broadcast Intent,再執行component,然後component去收Intent的順序

下面的範例需要2個Activity

Activity1.java
Activity2.java

我 們在Activity1中先sendStickyBroadcast(), 然後按去start Activity2,會發現Activity2可以收到剛才那個Intent。如果一開始是用sendBroadcast()的話,會發現按下start Activity2,是沒有辦法印出"This is Activity2 onReceive()"這段LOG的。

AndroidManifest.xml內容如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.stickyintent"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/ic_launcher">
        <activity
            android:name=".Activity1" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        <activity
            android:name=".Activity2" >
        </activity>           
    </application>

    <uses-permission android:name="android.permission.BROADCAST_STICKY"/>   
       
</manifest>


Activity1.java 程式如下:
public class Activity1 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
       
        OnClickListener lis = new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                switch (arg0.getId()) {
                case R.id.btn1:
                    btn1();
                    break;
                case R.id.btn2:
                    btn2();
                    break;
                }
            }   
        };
       
        Button btn1 = (Button) findViewById(R.id.btn1);
        Button btn2 = (Button) findViewById(R.id.btn2);
        btn1.setOnClickListener(lis);
        btn2.setOnClickListener(lis);
    }
   
    private void btn1(){
        Intent it = new Intent("com.test.stickyintent");
        sendStickyBroadcast(it);       
    }
   
    private void btn2(){
        Intent it = new Intent(this, Activity2.class);
        startActivity(it);
    }
}


Activity2.java程式如下:
public class Activity2 extends Activity {
    /** Called when the activity is first created. */
    private IntentFilter mIntentFilter;
   
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("gill", "This is Activity2 onReceive()");
        }
    };
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("gill","Activity2 onCreate()");
       
        setContentView(R.layout.main2);
        mIntentFilter = new IntentFilter();    
        mIntentFilter.addAction("com.test.stickyintent");   
    }
   
    @Override 
    protected void onResume() { 
        super.onResume(); 
        Log.d("gill","Activity2 onResume()");
        registerReceiver(mReceiver, mIntentFilter);  
    }
   
    @Override
    protected void onPause() {
        super.onPause();
        Log.d("gill","Activity2 onRause()");
        unregisterReceiver(mReceiver);
    }
}
程式畫面如下:






















執行結果LOG如下:
D/gill    ( 2443): Activity2 onCreate()
D/gill    ( 2443): Activity2 onResume()
D/gill    ( 2443): This is Activity2 onReceive()

沒有留言:

張貼留言