• Android(java)学习笔记121:BroadcastReceiver之 自定义广播


    广播使用:
                   电台:对外发送信号。---------电台发送广播(可以自定义)
                   收音机:接收电台的信号。-----广播接收者

    这里,我们就说明自定义电台,搭建属于自己的电台广播

    1. 首先我们编写自己的电台android 程序,搭建电台,如下:

    (1)首先是布局文件,activity_main.xml文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
        <Button
            android:onClick="click"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="发送广播" />
    
    </RelativeLayout>

    (2)上面给Button定义一个方法click,这里我们就直接实现它,点击按钮发送广播:

    则MainActivity.java代码如下:

    package com.itheima.sender;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        
        // 发送广播
        public void click(View view){
    Intent intent
    = new Intent(); intent.setAction("com.itheima.sender.CCTV"); intent.setData(Uri.parse("cctv://全国上下一片红")); sendBroadcast(intent); } }

    利用Intent发送数据,这里的Action动作为:字符串"com.itheima.sender.CCTV",这是自定义的。
    下面是设置数据为:intent.setData(Uri.parse("cctv://全国上下一片红"));这里自定义设置数据为Uri类型,数据开头关键字为:cctv

    这里的不用设置任何权限

    2. 上面搭建好电台,必须建立建立广播接收者,这样我们才能听到广播,如下我们再编写一个监听上面自定义广播的广播接收者android程序:

    (1)第一步还是买了收音机,编写MyBroadcastReceiver继承自BroadcastReceiver

    package com.itheima.receiver;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "我是公务员,我接收到了组织的广播"+intent.getData(), 0).show();
        }
    }

    (2)第二步和第三步是:装电池调频道

    在AndroidManifest.xml配置文件的application节点下添加receiver节点内容

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.itheima.receiver"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.itheima.receiver.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.itheima.receiver.MyBroadcastReceiver"> <intent-filter > <action android:name="com.itheima.sender.CCTV"/> <data android:scheme="cctv"/> </intent-filter> </receiver>

    </application> </manifest>

     在手机上的运行效果如下

    3. 上面已经搭建好了自定义的电台  和 同时也准备好了相应的广播接收者

    我们看看运行的效果,点击如下界面的按钮:

    按下按钮之后出现的效果如下:

    这就是完整的自定义电台的逻辑过程

  • 相关阅读:
    Android SwipeActionAdapter结合Pinnedheaderlistview实现复杂列表的左右滑动操作
    Android 使用SwipeActionAdapter开源库实现简单列表的左右滑动操作
    你读到了什么:谈谈阅读的空与实
    飞行的架构师和奔跑的程序员
    poj 3252 数位dp
    hdu 4734 数位dp
    hdu 2089 不要62 数位dp入门
    蓝桥杯模拟赛 青出于蓝而胜于蓝
    bzoj 4034
    hdu 3974 dfs时间戳+线段树
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4742490.html
Copyright © 2020-2023  润新知