Android BroadcastRecevier广播消息
来源:互联网 发布:淘宝导航条hot图标 编辑:程序博客网 时间:2024/06/16 05:26
代码注册Receiver
@Override protected void onCreate(Bundle savedInstanceState) { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(""); MyBroadcast myBroadcast = new MyBroadcast(); registerReceiver(myBroadcast, intentFilter); }
AndroidManifest.xml
<receiver android:name=".MyBroadcast"> <intent-filter> <action android:name="shortcut.song.com.myapplication.MY_BROADCAST"/> </intent-filter> </receiver>
MyBroadcast .java
package shortcut.song.com.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;/** * Created by Administrator on 2017/8/16 0016. */public class MyBroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "接收到的Intent Action为:"+intent.getAction() + " \n 消息内容:"+intent.getStringExtra("msg") , Toast.LENGTH_SHORT).show(); }}
<?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="shortcut.song.com.myapplication.BroadcastTestActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SendBroadcast" android:onClick="sendBroadCast"/></LinearLayout>
package shortcut.song.com.myapplication;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class BroadcastTestActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_broadcast_test); } public void sendBroadCast(View v) { Intent intent = new Intent(); intent.setAction("shortcut.song.com.myapplication.MY_BROADCAST"); intent.putExtra("msg", "简单的消息"); //发送广播 sendBroadcast(intent); }}
阅读全文
0 0
- Android BroadcastRecevier广播消息
- BroadcastRecevier(广播)
- Android - BroadcastRecevier
- BroadcastRecevier广播的基本使用
- Android——BroadcastRecevier
- 微子学Android之BroadcastRecevier
- Android使用BroadCastRecevier广播实现接收短信,并利用Toast弹出显示内容
- Android四大组件之BroadcastRecevier
- 【android】SD卡插拔 广播消息
- Android消息广播的使用
- Android:广播类别消息类型
- Android简单的广播消息
- Android广播消息及BroadcastReceiver
- Android演示如何接收广播消息
- Android动作广播类别消息类型
- android消息广播Intent传递数据
- android fragment间的广播消息接收
- android 限制广播消息的接收者
- 2017 多校7 1005 Euler theorem
- mysql最新版中文参考手册在线浏览
- php代码规范
- 配置SSH 无密码连接遇到的坑
- 数据MySQL占用空间数
- Android BroadcastRecevier广播消息
- glibc源码分析之access函数
- HDU 5542 The Battle of Chibi
- 15个开源的顶级人工智能工具
- Js数据类型的学习
- MQTT C Client实现消息推送(入门指南)
- 用C#开发简易小游戏之五子棋
- leetcode160. Intersection of Two Linked Lists
- Java中HashMap详解