Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android:靜態注冊BroadcastReceiver

Android:靜態注冊BroadcastReceiver

編輯:關於Android編程

由於動態注冊BroadcastReceiver只能在Activity的onCreate()方法調用時才能注冊再接收廣播,所以當程序沒有運行就不能接受到廣播;但是靜態注冊的則不依賴於程序是否處於運行狀態。

當然動態注冊的好處是,自由靈活。

下面來看一下靜態注冊的代碼吧:

MainActivity.java:

package com.example.staticbroadcastreceiverdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;

public class MainActivity extends Activity {

    private Handler myHandler;

    //定義一個Intent,用於發送廣播;
    private Intent myIntent;

    //子線程標志位;
    private boolean tag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        //實例化Intent
        myIntent = new Intent();
        //設置過濾條件;
        myIntent.setAction(com.vrinux.static);

        //此處我使用一個Handler,接收子線程每隔3秒發來一次消息,
        //就發送一個廣播,並將值發出去;
        myHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                super.handleMessage(msg);
                int count = msg.arg1;
                myIntent.putExtra(text, 來自activity的廣播~ + count);

                //發送廣播;
                sendBroadcast(myIntent);
            }
        };

        new Thread(new Runnable() {

            int count = 0;

            @Override
            public void run() {
                // TODO Auto-generated method stub
                //將標志位值設為true;
                tag = true;
                while (tag) {
                    Message msg = Message.obtain();
                    msg.arg1 = count;
                    myHandler.sendMessage(msg);

                    SystemClock.sleep(3000);

                    count += 1;
                }
            }
        }).start();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        //當Activity銷毀時,將標志位設置為false;
        //如果不設置為false,則會看到程序退出後手機屏幕上仍然有Toast顯示信息;
        //從這點可以看出:1.線程啟動後不會隨著Activity銷毀而銷毀;
        //                      2.靜態注冊的廣播比依賴於程序是否處於運行狀態;
        if(tag){
            tag = false;
        }       
    }
}

PlaneBroadcastReceiver.java:

package com.example.staticbroadcastreceiverdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

//繼承BroadcastReceiver類;
public class PlaneBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.i(MainActivity,PlaneBroadcastReceiver);

        //獲取intent傳來的值;
        String text = intent.getStringExtra(text);

        //通過Toast顯示在屏幕上;
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();;
    }

}

 



    

    
        
            
                

                
            
        

        
        
            
                
            
        
    

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved