Android中 广播发送 和 接受 的简单示例
来源:互联网 发布:腾讯游戏2017数据分析 编辑:程序博客网 时间:2024/05/17 06:34
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcast" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.broadcast.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyReceiver"> <intent-filter > <action android:name="com.example.BROADCAST"/> </intent-filter> </receiver> </application></manifest>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/sendbroad" android:layout_width="match_parent" android:layout_height="90dp" android:text="发送广播"/></RelativeLayout>
package com.example.broadcast;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity { private Button sendbt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendbt = (Button) findViewById(R.id.sendbroad); sendbt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("com.example.BROADCAST"); intent.putExtra("msg", "这是广播发送的消息"); sendBroadcast(intent); } }); }}
MyReceiver
package com.example.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "接收到的Intent的Action位:"+intent.getAction()+"\n消息内容是:"+intent.getStringExtra("msg"),Toast.LENGTH_LONG).show(); }}
效果如图
0 2
- Android中 广播发送 和 接受 的简单示例
- android中BroadCastReceiver使用(广播的接受和发送)
- Android广播的发送和接受
- android 随手记 广播的发送和接受
- android 随手记 广播的发送和接受
- 接受和发送广播
- 接受和发送广播
- Android中短信的接受广播和短信的解析
- (八)React Native实现调用android原生java方法并实现广播的发送和接受
- 广播的发送和接受过程是怎样的
- Android发送权限受限的广播,指定接受方!
- android 跨应用程序广播发送接受
- 跨应用发送和接受广播
- android 接受消息广播,简单的自定义notification样式弹出
- Android Broadcast 的接受和发送
- 非常简单的android广播示例
- Android 广播的传递和接受 动态注册 基本用法
- Android 广播的传递和接受 动态注册 基本用法
- 判断一个数是否是立即数
- Windows配置Redmine运行环境
- 体会棋盘游戏中的数据存储
- ActionBar
- 第14周项目6-8-数组反转
- Android中 广播发送 和 接受 的简单示例
- 杭电 1070
- 应用启动时发生“The network connection was lost.”错误
- 十一.Eclipse集成开发环境
- golang中goconfig包使用解析
- 蓝桥杯中的砝码组合问题
- linux下core文件调试方法
- 编程:带姓名的成绩单(1)
- 软件工程基础了解