zoukankan      html  css  js  c++  java
  • Android基础——广播(静态注册)

    安卓版本高了就会有点问题,不能静态注册 

    令活动Main用来发广播,另一个接收器(不是Activity而是receiver)用来接收广播

    注册文件

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.mybroadi">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <receiver android:name=".Myreceiver"
                android:enabled="true"
                android:exported="true">
                <intent-filter>
                    <action android:name="com.example.mybroadi.bd"/>
                </intent-filter>
            </receiver>
        </application>
    
    </manifest>

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送广播"/>
    
    
    </LinearLayout>

    java代码

    接收器

    package com.example.mybroadi;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class Myreceiver extends BroadcastReceiver {
        private static final String ACTION1 = "com.example.mybroadi.bd";
        private static final String ACTION2 = "";
    
        //广播接收器,还要去注册一下这个接收器
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"ok",Toast.LENGTH_SHORT).show();
            if(intent.getAction().equals(ACTION1)){
                Toast.makeText(
                        context,"收到"+ACTION1,Toast.LENGTH_SHORT
                ).show();
            }
            else {
                Toast.makeText(
                        context,"收到"+ACTION2,Toast.LENGTH_SHORT
                ).show();
            }
        }
    }

    Main

    package com.example.mybroadi;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button button1 = (Button)findViewById(R.id.button1);
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //发送一条广播
                    Intent intent = new Intent("com.example.mybroadi.bd");
                    sendBroadcast(intent);//发送广播
                }
            });
        }
    }
  • 相关阅读:
    Castle.Aop.Autofac
    signalR 在webfarm下的配置
    SQL语句中 string类型数字的比较
    access 查询空字段
    c#利用jmail发送邮件
    C# 利用Jmail接收邮件
    Asp.net 使用 AXAJ局部刷新无效的解决方法
    把查询的数据放入多维数组中
    获取网站的根目录的物理文件系统路径
    C#.net如何生成静态页带母板的那种
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12244184.html
Copyright © 2011-2022 走看看