频道栏目
IT货架 > > 正文
Android:静态注册BroadcastReceiver
网友分享于:Jan 1, 1970 8:00:00 AM    来源: IT货架   

标签:android   广播   broadcastreceiver   动态注册   

动态注册的广播接收器可以自由地控制注册与注销,在灵活性方面有很大的优势,但是它也存在着一个缺点,即必须要在程序启动之后才能接收到广播,因为注册的逻辑是写在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();;
    }

}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.staticbroadcastreceiverdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.staticbroadcastreceiverdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- 注册BroadcastReceiver,并设置过滤器和过滤条件 -->
        <receiver
            android:name=".PlaneBroadcastReceiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="com.vrinux.static" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

Android:静态注册BroadcastReceiver

标签:android   广播   broadcastreceiver   动态注册   

相关板块推荐 iPhone WindowsPhone

广告服务联系QQ:1134687142 | 网站地图

版权所有: IT货架- 内容来自互联网,仅供用于技术学习,请遵循相关法律法规. 京ICP备11030978号-1