注册一个静态广播接收器,接收系统启动广播时没反应

该博客围绕Android开发,介绍了广播接收器相关程序设计。包括注册静态广播接收器接收系统启动广播并自动运行程序,在界面设置按钮发送广播、TextView显示广播效果,还提及新建广播接收器类、在主配置文件注册接收器及设计程序界面等内容。

题目:

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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值