概述
Android系统发起许多系统范围的广播通知,如屏幕关闭,电池电量低。此外,应用程序可以启动“由开发人员定义”广播的自定义。广播是在应用程序或其他应用程序中发送消息的一种方式。一个BroadcastReceiver是侦听和响应发起的广播的组件。本博文将学习如何简单发送自定义广播以及如何以编程方式声明广播接收者,并使用清单文件来收听广播。
目标:
- 如何发送广播?
- 如何创建广播接收者子类?
- 如何在Java代码或清单文件中注册接收器?
(1)创建布局“UI”
- res/layout/activity_main.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="Received Broadcasts" />
<Button
android:id="@+id/btnSendBroadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="Send Broadcast"
/>
<EditText
android:id="@+id/etReceivedBroadcast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
(2)扩展BroadcastReceiver
- 创建一个扩展名称为MyReceiver的子类BroadcastReceiver。
- 覆盖onReceive()方法在EditText上打印intent操作名称
- /src/com/zhaolei/android/MyReceiver.java
package com.zhaolei.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
MainActivity mainActivity = ((MyApplication) context.getApplicationContext()).mainActivity;
mainActivity.etReceivedBroadcast.append("broadcast: "+intent.getAction()+"n");
}
}
(3)发送广播
- 要发送广播,创建一个具有动作名称的Intent并将其传递给sendBroadcast()
Intent i = new Intent(“com.zhaolei.android.USER_ACTION”);
sendBroadcast(ⅰ);
(4)注册BroadcastReceiver“Java或XML”
您可以在Java代码或Manifest XML文件中动态注册boradcast接收器。
在Java代码中
- 创建广播接收器类“的实例MyReceiver的”和实例的IntentFilter并传递给registerReceiver()方法。
- 在Activity onResume()方法中调用registerReceiver() 。
@Override
protected void onResume() {
super.onResume();
registerReceiver(new MyReceiver(),
new IntentFilter("com.zhaolei.android.USER_ACTION"));
}
- 或者修改Manifest.xml文件
<receiver android:name="com.zhaolei.android.MyReceiver" >
<intent-filter>
<action android:name="com.zhaolei.android.USER_ACTION" />
</intent-filter>
</receiver>
(5)活动类
- /src/com/zhaolei/android/MainActivity.java
package com.zhaolei.android;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
MyReceiver myReceiver;
IntentFilter intentFilter;
EditText etReceivedBroadcast;
Button btnSendBroadcast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etReceivedBroadcast = (EditText) findViewById(R.id.etReceivedBroadcast);
btnSendBroadcast = (Button) findViewById(R.id.btnSendBroadcast);
MyApplication myApplication = (MyApplication) this.getApplicationContext();
myApplication.mainActivity = this;
btnSendBroadcast.setOnClickListener(this);
myReceiver = new MyReceiver();
intentFilter = new IntentFilter("com.zhaolei.android.USER_ACTION");
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(myReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(myReceiver);
}
@Override
public void onClick(View view) {
Intent intnet = new Intent("com.zhaolei.android.USER_ACTION");
sendBroadcast(intnet);
}
}
(6)Manifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zhaolei.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:name="com.zhaolei.android.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.zhaolei.android.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="com.zhaolei.android.MyReceiver" >
<intent-filter>
<action android:name="com.zhaolei.android.USER_ACTION" />
</intent-filter>
</receiver>
</application>
</manifest>
- 注意:如果要使用Manifest XML文件注册接收器,请从MainActivity类中删除onResume和onPause方法
(7)获取活动上下文解决方法
当我们使用XML注册接收器时,系统将ReceiverResctrictredContext传递给onReceive()方法。要访问MainActivity上下文以便在EditText“etReceivedBroadcast”上打印,我们需要通过扩展我们自己的Application类来解决这个问题,该类保留对我们的MainActivity类的引用。
package com.zhaolei.android;
import android.app.Application;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
}
MainActivity mainActivity;
}
- 注意我们在Manifest文件中使用了MyApplication
<application
android:name="com.zhaolei.android.MyApplication"
最后
以上就是洁净小蘑菇为你收集整理的Android 发送和接收定制广播的全部内容,希望文章能够帮你解决Android 发送和接收定制广播所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复