题目:
1. 注册一个静态广播接收器,接收系统启动广播,收到系统启动的广播后,自动运行程序
2. 在界面上设置1个按钮,分别对应发送1种广播
2. 在界面上设置1个TextView,用来显示接收到不同广播后的效果
3. 在代码中实现界面组件预设功能


注意:不加双引号会报错

.新建一个广播接收器类,该类在收到系统启动的广播后,将会启动本程序
package com.qst.chapter05.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by Adminstrator
* BroadcastReceiver 实例
* 接收端
*/
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();
}
}
.在主配置文件中,注册一个广播接收器,并添加接收系统启动的广播
<receiver android:name=".activity.MyReceiver">
<intent-filter>
<action android:name="org.crazyit.action.CRAZY_BROADCAST" />
</intent-filter>
</receiver>
为程序设计界面,broadcastreceiver_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/send"
/>
</LinearLayout>
该博客围绕Android开发,介绍了广播接收器相关程序设计。包括注册静态广播接收器接收系统启动广播并自动运行程序,在界面设置按钮发送广播、TextView显示广播效果,还提及新建广播接收器类、在主配置文件注册接收器及设计程序界面等内容。

1342

被折叠的 条评论
为什么被折叠?



