Android控件BroadcastReceiver详解
来源:互联网 发布:皮书包知乎 编辑:程序博客网 时间:2024/06/15 01:47
1.BroadcastReceiver控件详解
2.代码实现
a.接收广播代码实现
package com.example.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyBroadCastReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {Toast.makeText(context,"接收到的Intent的Action为: "+intent.getAction()+"\n消息内容是: "+intent.getStringExtra("msg"),Toast.LENGTH_LONG).show();System.out.println("onCreate CurrentThread = " + Thread.currentThread().getName());}}
b.发送广播的控件Activity的代码实现
package com.example.broadcastreceivertest;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 {Button send;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取程序界面中的按钮send=(Button) findViewById(R.id.send);//事件绑定send.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {//启动BroadcastReceiver事件的intent,intent信息的解析Intent intent=new Intent();intent.setAction("com.example.broadcastreceivertest.START_BroadcastReceiver");intent.putExtra("msg","你大爷的!!!!!");//发送广播sendBroadcast(intent);}});}}
c.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceivertest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.example.broadcast.MyBroadCastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.example.broadcastreceivertest.START_BroadcastReceiver"/>
</intent-filter>
</receiver>
<activity
android:name="com.example.broadcastreceivertest.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>
</application>
</manifest>
- Android控件BroadcastReceiver详解
- android控件24 BroadcastReceiver
- Android BroadcastReceiver 详解
- android 广播(broadcastReceiver)详解
- Android BroadcastReceiver应用详解
- 详解 Android 中 BroadcastReceiver
- Android BroadcastReceiver 详解
- Android BroadcastReceiver广播详解
- Android BroadcastReceiver详解
- android BroadcastReceiver应用详解
- Android中BroadCastReceiver详解
- Android中BroadCastReceiver详解
- Android BroadcastReceiver应用详解
- Android基础BroadcastReceiver详解
- Android--BroadcastReceiver应用详解
- Android--BroadcastReceiver应用详解
- Android--BroadcastReceiver应用详解
- Android-BroadcastReceiver详解
- UVa 1185 - Big Number
- 操作系统多级反馈轮转法的c语言模拟
- 代码整洁之道 函数
- mysql 日志详解
- Guide of Installing CUDA-7.5 on Ubuntu 14.04
- Android控件BroadcastReceiver详解
- 小白编程之使用友盟自动更新
- 5.5 - Pass by Reference
- 抽象类
- SQL学习:SQLCookBook源代码-mysql版本(2)
- 结构体字长计算方法
- 非常棒的二分查找所有情况的考虑
- uva 1368 DNA Consensus String
- linux图形界面登陆失败