Android(安卓)BroadcastReceiver实例Demo
16lz
2021-01-25
该文主要讲解Android四大组件之一:BroadcastReceiver的初步使用,和其他三大组件一样,都需要在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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic 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 {@Overridepublic 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;@Overridepublic 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 {@Overridepublic 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>
比较简单,需要源码的读者可以到我的资源中下载。
更多相关文章
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- Android(安卓)Quick Tips #2 — Android(安卓)Studio Productivi
- android应用安全――组件通信安全(Intent)
- Android(安卓)中使用Protocol Buffer
- 【Arcgis for android】spatialite打开shapefile
- Android(安卓)Jetpack之生命周期的处理