• Broadcast Reveiver作用


    Broadcast Reveiver作用以及为何要引入(用来接收系统以及自定义消息的)
    在系统内通知和判定执行状态
    1,系统执行状态,开机了,TF卡插拔,准备关机,电量低了,
    2,自定义执行状态,发消息提示更新界面

    点击后会打log:顺序是testbroadcast2,testbroadcast
    activity代码-----
    package com.paoyx;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    public class BroadcastActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            //设置按钮
            Button button = new Button(this);
            button.setText("send message");
            setContentView(button);
            button.setOnClickListener(new OnClickListener() {
    			//按钮点击事件
    			public void onClick(View v) {
    				//设置intent传递数值 
    				Intent intent = new Intent();
    				intent.setAction("com.paoyx.broadcast");
    				sendBroadcast(intent);
    			}
    		});
        }
    }

    2个receiver代码---

    package com.paoyx;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    public class MyReceiver extends BroadcastReceiver {
    	public void onReceive(Context context, Intent intent) {
    		// TODO Auto-generated method stub
    		if (intent.getAction().equals("com.paoyx.broadcast")) {
    			Log.i("log","testbroadcast");
    		}
    	}
    }

    package com.paoyx;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    public class MyReceiver2 extends BroadcastReceiver {
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		// TODO Auto-generated method stub
    		if (intent.getAction().equals("com.paoyx.broadcast")) {
    			Log.i("log","testbroadcast2");
    		}
    	}
    }

    menifest注册监听代码---
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.paoyx"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-sdk android:minSdkVersion="8" />
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".BroadcastActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            //注册receiver,并设置优先级别,数值大的优先,所以先启动MyReceiver2
            <receiver android:name=".MyReceiver">
                <intent-filter android:priority="1">
                    <action android:name="com.paoyx.broadcast"/>
                </intent-filter>
            </receiver>
             <receiver android:name=".MyReceiver2">
                <intent-filter android:priority="2">
                    <action android:name="com.paoyx.broadcast"/>
                </intent-filter>
            </receiver>
        </application>
    </manifest>


  • 相关阅读:
    关于gc日志中Desired Survivor的疑问和对象晋升老年代的小结
    Tomcat 中部署 web 应用 ---- Dubbo 服务消费者 Web 应用 war 包的部署
    10种常见的排序算法
    让我们来谈谈JDBC
    单例的设计模式
    使用putty与SSHSecureShellClient登录远程服务器完成与本地Git项目的同步
    安装 Dubbo 管理控制台
    邮件工具类
    Hadoop系列教程<一>---Hadoop是什么呢?
    setTimeout闭包常见问题
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3196851.html
Copyright © 2020-2023  润新知