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);//发送广播
            }
        });
    }
}

 

你可能感兴趣的:(Android基础——广播(静态注册))