Android基础——广播(静态注册)

摘要:
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基础——广播(静态注册)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇jqGrid 常用 总结 -1UILabel的顶对齐解决方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android设置点击变色效果

首先在drawable下面新建一个select.xml文件,代码如下: 1 <?xml version="1.0" encoding="utf-8"?> 2 <selector xmlns:android="http://schemas.android.com/apk/res/android" > 3 <item an...

Unity3d与android通信

                                                   Unity3d与android通信                                                                                                           @广州小...

Android学习——后台程序

Android学习——后台程序 在Android系统中我们一直在接触着前台界面程序,其实在一开始接触Android时就听说了,程序就有有界面和无界面之分。后台程序就是这类无界面的程序,它在后台执行,没有影响你的界面。比如短信监听程序,执行在后台,当有短信时才给你们提示,振动或声音;比如闹钟,设定好时间后,在定时通知你;再比如mp3播放器,选择好音乐后,在待...

Android DeepLink 深度链接技术实现

一、DeepLink 技术介绍 DeepLink,即为深度链接技术,主要应用场景是通过Web页面直接调用Android原生app,并且把需要的参数通过Uri的形式,直接传递给app,节省用户的注册成本。 DeepLink 通常运用于App社交分享、App广告引流、App裂变活动、Web to App、分享效果统计、沉默用户唤醒等场景,对广告引流、活动推广、...

安卓学习第18课——AdapterViewFlipper

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" androi...

android通过webview调起支付宝app支付

webview在加载网页的时候会默认调起手机自带的浏览器加载网页,用户体验不好。但当用户设置浏览器客户端(setWebViewClient)设置这样的监听事件之后,当请求url的时候就不会打开手机自带的浏览器。 webview.setWebViewClient(new WebViewClient() { @Override...