• android 48 广播


    系统开始重启会发送开机重启广播,电量低的时候会发送电量低的广播,广播注册有2种:系统说明文件xml注册和Java代码注册,前者是静态注册(全局注册)后者是动态注册(依赖于当时组建,组件销毁就收不到广播了)。

     Activity.java

    package com.sxt.day07_04;
    
    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    
    public class MainActivity extends Activity {
        MyReceiver2 mReceiver;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            registerReceiver();
            setListener();
        }
    
        private void setListener() {
            sendBroadcast1ClickListener();
            sendBroadcast2ClickListener();
        }
    
        private void sendBroadcast2ClickListener() {
            findViewById(R.id.btnSendBroadcast_2).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent("com.sxt.day07_04.MyReceiver2");//
                    intent.putExtra("key", "hello android!");
                    sendBroadcast(intent);
                }
            });
        }
    
        private void sendBroadcast1ClickListener() {
            findViewById(R.id.btnSendBroadcast_1).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent("com.sxt.day07_04.MyReceiver1");
                    intent.putExtra("key", "hello java!");
                    sendBroadcast(intent);
                }
            });
        }
    
        //java类注册的广播接收者
        private void registerReceiver() {
            mReceiver=new MyReceiver2();
            IntentFilter filter=new IntentFilter();//意图过滤器
            filter.addAction("com.sxt.day07_04.MyReceiver2");//广播的字符串
            registerReceiver(mReceiver, filter);
        }
    
        class MyReceiver2 extends BroadcastReceiver{
            @Override
            //收到广播执行的方法
            public void onReceive(Context context, Intent intent) {
                Log.i("main","MyReceiver2.onReceive():"+intent.getStringExtra("key"));
            }
        }
        
        @Override
        protected void onDestroy() {
            super.onDestroy();
            //Activity退出的时候取消广播的注册,释放资源.
            unregisterReceiver(mReceiver);
        }
    }

    广播1.java

    package com.sxt.day07_04;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    public class MyReceiver1 extends BroadcastReceiver {
    
        @Override
        //收到广播后执行的方法
        public void onReceive(Context context, Intent intent) {
            Log.i("main","MyReceiver1.onReceive():"+intent.getStringExtra("key"));
        }
    
    }

    系统描述文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.sxt.day07_04"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.sxt.day07_04.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>
            注册全局广播接收者
            <receiver android:name="com.sxt.day07_04.MyReceiver1">   广播接收者类
                <intent-filter>
                    <action android:name="com.sxt.day07_04.MyReceiver1"/>   广播的字符串是这个就可以接收广播
                </intent-filter>
            </receiver>
        </application>
    
    </manifest>
  • 相关阅读:
    Linux系统 SecureCRT SecureFX 注册破解方法
    局域网代理通过wget下载
    Package gtk+-3.0 was not found in the pkg-config search path
    Js获取当前系统时间
    vuex-- Vue.的中心化状态管理方案(vue 组件之间的通信简化机制)
    HTML空格占位符
    vue表单验证--veevalidate使用教程
    vue 时间选择器组件
    js处理数据库时间格式/Date(1332919782070)/
    vue-cli 自定义过滤器的使用
  • 原文地址:https://www.cnblogs.com/yaowen/p/4890767.html
Copyright © 2020-2023  润新知