• 035 Android 广播(BroadCastReceiver)


    1.介绍

    2.实现方法

    3.注册广播

    (1)静态广播

    在AndroidManifest.xml文件中注册广播

    <intent-filter>为过滤器
    <receiver android:name=".MyBroadCast1"></receiver>
            <receiver android:name=".MyBroadCast2">
                <intent-filter>
                    <action android:name="com.lucky"></action>
                </intent-filter>
            </receiver>

    (2)动态广播

    4.静态广播代码

    (1)主界面

    package com.lucky.test40broadcastreceiver;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
        Button button1;
        Button button2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button1=findViewById(R.id.button);
            button2=findViewById(R.id.button2);
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MainActivity.this,MyBroadCast1.class);
                    sendBroadcast(intent); //发送广播
                }
            });
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //设置Action为com.lucky,会在AndroidManifest.xml中的receiver中启动action为com.lucky的广播
                    Intent intent=new Intent();
                    intent.setAction("com.lucky");
                    sendBroadcast(intent);
                }
            });
        }
    }

    (2)广播接收器1(采用Toast语句进行提示)

    package com.lucky.test40broadcastreceiver;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    //MyBroadCast1广播接收器
    public class MyBroadCast1 extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"接收到广播",Toast.LENGTH_SHORT).show();
        }
    }

    (3)广播接收器2(采用通知的方式进行提示)

    package com.lucky.test40broadcastreceiver;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Build;
    import android.support.annotation.RequiresApi;
    
    public class MyBroadCast2 extends BroadcastReceiver {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onReceive(Context context, Intent intent) {
            //采用通知的形式
            NotificationManager manager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification.Builder builder=new Notification.Builder(context);
            builder.setSmallIcon(R.mipmap.ic_launcher); //给通知设置图标
            builder.setTicker("广播消息"); //设置提示消息
            builder.setContentTitle("您有一条广播消息");//设置内容标题
            builder.setContentText("hello lucky");    //设置通知内容
            manager.notify(0x01,builder.build()); //让通知显示
        }
    }

    5.动态广播

    (1)主界面

    package com.lucky.test41broadcastreceiver2;
    
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
        Button button1;
        MyBroadCast myBroadCast;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button1=findViewById(R.id.button);
    
            //注册广播
            myBroadCast=new MyBroadCast(); //实例化广播接收器
            IntentFilter intentFilter=new IntentFilter();//实例化一个过滤器
            intentFilter.addAction("com.lucky"); //添加action来明确接收哪种类型的广播
            registerReceiver(myBroadCast,intentFilter);//注册广播
    
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //发送广播
                    Intent intent=new Intent();
                    intent.setAction("com.lucky");
                    sendBroadcast(intent);
                }
            });
        }
    }

    (2)广播接收器

    package com.lucky.test41broadcastreceiver2;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class MyBroadCast extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"hello lucky",Toast.LENGTH_SHORT).show();
        }
    }
  • 相关阅读:
    web总结
    工作总结
    python积累
    学习地图
    position:absolute绝对定位解读
    利用C++ RAII技术自动回收堆内存
    C++封装常用对象和对头文件探索
    String.split()方法你可能不知道的一面
    动态内存分配(new)和释放(delete)
    C#实现的异步Socket服务器
  • 原文地址:https://www.cnblogs.com/luckyplj/p/10523535.html
Copyright © 2020-2023  润新知