Android BroadcastReceiver实例Demo
2014-07-15 18:00
169 查看
该文主要讲解Android四大组件之一:BroadcastReceiver的初步使用,和其他三大组件一样,都需要在AndroidManifest.xml文件中注册(当然,广播注册的另一种方式是在代码中)。
实现效果图:
源代码:
布局文件:
MainActivity:
MyBroadcastReceiver:
MyBroadcastReceiver2:
MyBroadcastReceiver3:
AndroidManifest.xml:
实现效果图:
源代码:
布局文件:
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="点击发送广播" /> </RelativeLayout>
MainActivity:
package com.android_broadcastreceiver; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); /** * 三个广播在manifest.xml文件中的Action设置的都是myaction,所以发送广播的时候,这三个都可以收到广播 * 。 */ intent.setAction("myaction"); intent.putExtra("name", "梅西!!!"); sendBroadcast(intent); } }); } }
MyBroadcastReceiver:
package com.android_broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String name = intent.getExtras().getString("name"); Toast.makeText(context, "广播1已发送..."+name, Toast.LENGTH_SHORT).show(); } }
MyBroadcastReceiver2:
package com.android_broadcastreceiver; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; /** * 以通知的形式展示广播 * */ public class MyBroadcastReceiver2 extends BroadcastReceiver { private NotificationManager manager; @Override public void onReceive(Context context, Intent intent) { manager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); String name = intent.getExtras().getString("name"); NotificationCompat.Builder builder = new NotificationCompat.Builder( context); builder.setTicker("广播2来了!!!"); builder.setSmallIcon(R.drawable.ic_launcher); builder.setContentTitle("电量不足!!!"); builder.setContentText("电量还剩10%"+"快通知:"+name); manager.notify(1001, builder.build()); } }
MyBroadcastReceiver3:
package com.android_broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyBroadcastReceiver3 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String name = intent.getExtras().getString("name"); Toast.makeText(context, "广播3已发送..."+name, Toast.LENGTH_SHORT).show(); } }
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android_broadcastreceiver" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" 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.android_broadcastreceiver.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=".MyBroadcastReceiver" > <intent-filter> <action android:name="myaction" /> </intent-filter> </receiver> <receiver android:name=".MyBroadcastReceiver2" > <intent-filter> <action android:name="myaction" /> </intent-filter> </receiver> <receiver android:name=".MyBroadcastReceiver3" > <intent-filter> <action android:name="myaction" /> </intent-filter> </receiver> </application> </manifest>
比较简单,需要源码的读者可以到我的资源中下载。
相关文章推荐
- [Android实例] android多点触摸demo .
- [Android实例] android多点触摸demo
- Android实例demo5之FileOutputStream文件写入的多种方法
- Android实例demo10之listview的使用(加载xml、textview、BaseAdapter的使用)
- Android实例demo9之sqlite中一些要注意的地方 代码执行时间
- IPC analysis on android with a demo (基于IPC实例分析android IPC机制)
- Android实例demo14之查看网络图片、根据编码格式查看html
- Android实例demo1之查看手机内存状态、时间数据大小格式化
- Android BroadcastReceiver实例Demo(有序广播的发送)
- Android实例demo12之contentprovide内容提供者与被提供者
- Android 使用bundle在activity之间交换数据 DEMO (填写注册信息实例)
- Android HTTP实例:发送请求和接收响应的小demo
- [Android实例] android进度条对话框Demo
- Android实例demo4之标题、线程发短信
- Android ListFragment实例Demo(自定义适配器)
- Android 拖动条(SeekBar)实例 附完整demo项目代码
- Android和FTP服务器交互,上传下载文件(实例demo)
- Android Sensor Demo Framework实例
- Android实例demo7之xml保存pull解析
- Android实例demo13之短信备份、短信自定义