首页 > 其他分享 >安卓 广播消息

安卓 广播消息

时间:2023-03-14 12:35:25浏览次数:36  
标签:安卓 content 广播 Intent 消息 import android message


发送有序广播

1 单个广播 触发

安卓 广播消息_android studio

AndroidManifest.xml

<!-- 有序广播接收器使用的权限 -->
<uses-permission android:name="my.permission.receiver"></uses-permission>


<receiver android:name=".MyReceiver">
<intent-filter android:priority="-1000">
<action android:name="com.example.androd_8_onderedreceiver_Action_receiver"></action>
</intent-filter>
</receiver>

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn1 = (Button)findViewById(R.id.btn1);

// 发送有序广播---静态注册
btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

Intent intent = new Intent();
intent.setAction("xyz.lgch.onderedreceiver_Action_receiver");
intent.putExtra("message", "发送有序广播 www.lgch.xyz");

sendOrderedBroadcast(intent, "my.permission.receiver");

}
});
}

MyReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.widget.Toast;

// 创建 接收广播
public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
String message = "广播信息" + arg1.getStringExtra("message");
Toast toast = Toast.makeText(context, message, 3000);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
}

}

2 广播 传递 (暂时这么说)

触发广播2后,动态广播接收到参数后 触发
MainActivity.java

<!-- 发送广播的自定义权限 -->
<permission android:name="my.permission.receiver"></permission>

<!-- 有序广播接收器使用的权限 -->
<uses-permission android:name="my.permission.receiver"></uses-permission>
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button btn1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn1 = (Button) findViewById(R.id.btn1);

// 动态注册
IntentFilter intentFilter = new IntentFilter();
intentFilter
.addAction("xyz.lgch.onderedreceiver_Action_receiver");
intentFilter.setPriority(0);

registerReceiver(new MyReceiver5(), intentFilter);

// 发送有序广播---静态注册
btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

Intent intent = new Intent();
intent.setAction("xyz.lgch.onderedreceiver_Action_receiver");
intent.putExtra("message", "发送有序广播 www.lgch.xyz");

sendOrderedBroadcast(intent, "my.permission.receiver");

}
});
}

}

MyReceiver2.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;

public class MyReceiver2 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
String message = "广播信息:" + arg1.getStringExtra("message");
Toast toast = Toast.makeText(context, message, 3000);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();

Bundle bundle = new Bundle();

bundle.putString("aa","666");
setResultExtras(bundle);
}
}

MyReceiver.java

package com.example.testapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;

public class MyReceiver5 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {

Bundle bundle = getResultExtras(true);
String data = bundle.getString("aa");

String message = "广播信息" + arg1.getStringExtra("message");
Toast toast = Toast.makeText(context, message + "---" + data, 3000);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();

}
}


标签:安卓,content,广播,Intent,消息,import,android,message
From: https://blog.51cto.com/u_16006123/6120311

相关文章

  • 安卓菜单选项 Popup_Menu / Context_Menu / Option_Menu
    Option_Menu右上角的菜单optionmenu.xml这个页面没啥用<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.......
  • 安卓 外部存储/ExternalStorage 的使用
    权限<uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>将数......
  • 安卓 下载网页
    权限访问网络权限+外部存储权限<uses-permissionandroid:name="android.permission.INTERNET"/><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STOR......
  • 安卓 存储---SharedPreferences
    导包importandroid.content.SharedPreferences;importandroid.content.SharedPreferences.Editor;存储数据//创建应用级首选项SharedPreferencessharedPreferences=ge......
  • 安卓 静态文件读取 staticFile
    //静态文件的读取Resourcesresources=getResources();InputStreamopenRawResource=resources.openRawResource(R.raw.text);try{byte[]byte1=newbyte[openRawR......
  • 安卓 listView 组件的使用
    xml<ListViewandroid:id="@+id/list1"android:layout_width="wrap_content"android:layout_height="wrap_content"/>javaListViewlistView1=(ListView)......
  • 【快应用】推送消息token不合法案例
    ​ 【关键字】推送消息、快应用 【问题背景】快应用通过华为agc后台网站发送push消息时,填入token后提示所有的token不合法,该如何处理?快应用的token:I/jsLog(24193......
  • 【快应用】推送消息token不合法案例
    【关键字】推送消息、快应用【问题背景】快应用通过华为agc后台网站发送push消息时,填入token后提示所有的token不合法,该如何处理?快应用的token:I/jsLog(24193):push.subscr......
  • RabbitMQ实现延迟发送消息
    前言最近在做一个可以根据用户选择的时间,实现微信推送订阅消息的功能,突然想到rabbitmq好像可以实现这个功能,本着试试的心态开始研究,第一个想到的就是使用死信队列死信队列何......
  • Windows 10安装安卓子系统(WSA)最详细教程
    WSA forWindows10下载地址https://pan.baidu.com/s/15rUmm7UsSSY6BIo2UD9MIQ提取码:c97x 要想在Win10系统中正常运行安卓子系统,首先要保证你爱机的系统是“22H2......