Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 636|回复: 0

Android 深入解析用户界面(十一) - Android学习

[复制链接]

该用户从未签到

发表于 2011-10-26 19:00:58 | 显示全部楼层 |阅读模式
       一、Broadcast Receiver简介

  Android中的四大组件是 Activity、Service、Broadcast和Content Provider。而Intent是一个对动作和行为的抽象描述,负责组件之间程序之间进行消息传递。那么Broadcast Receiver组件就提供了一种把Intent作为一个消息广播出去,由所有对其感兴趣的程序对其作出反应的机制。

  二、Broadcast Receiver接收系统自带的广播

  我们做一个例子,功能是在系统启动时播放一首音乐。
  1、建立一个项目Lesson21_BroadcastReceiver,拷贝一首音乐进res/raw目录
  2、建立HelloBroadcastReceiver。java 内容如下:

java代码:
import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.media.MediaPlayer;

import android.util.Log;



public class HelloBroadReciever extends BroadcastReceiver {



//如果接收的事件发生

@Override

public void onReceive(Context context, Intent intent) {

//则输出日志

Log.e("HelloBroadReciever", "BOOT_COMPLETED!!!!!!!!!!!!!!!!!!!!!!!!!");

Log.e("HelloBroadReciever", ""+intent.getAction());



//则播放一首音乐

MediaPlayer.create(context, R.raw.babayetu).start();

}

}
复制代码
       3、在AndroidManifest.xml中注册此Receiver :

java代码:
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionname="1.0" android:versioncode="1" package="android.basic.lesson21">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:label="@string/app_name" android:name=".MainBroadcastReceiver">

<intent -filter="">

<action android:name="android.intent.action.MAIN">

<category android:name="android.intent.category.LAUNCHER">

</category></action></intent>

</activity>

<!-- 定义Broadcast Receiver 指定监听的Action -->

<receiver android:name="HelloBroadReciever">

<intent -filter="">

<action android:name="android.intent.action.BOOT_COMPLETED">

</action></intent>

</receiver>

</application></manifest>
复制代码
       三、自定义广播

  下面我们学习自己制作一个广播.我们接着刚才的例子,继续写下去.
  5、在MainBroadcastReceiver.java中填写如下代码:

java代码:
import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;



public class MainBroadcastReceiver extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);



Button b1 = (Button) findViewById(R.id.Button01);



b1.setOnClickListener(new View.OnClickListener() {



@Override

public void onClick(View v) {

//定义一个intent

Intent intent = new Intent().setAction(

"android.basic.lesson21.Hello").putExtra("yaoyao",

"yaoyao is 189 days old ,27 weeks -- 2010-08-10");

//广播出去

sendBroadcast(intent);

}

});

}

}
复制代码
       6、更改 HelloBroadReceiver.java 内容如下:

java代码:
import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.media.MediaPlayer;

import android.util.Log;



public class HelloBroadReciever extends BroadcastReceiver {



//如果接收的事件发生

@Override

public void onReceive(Context context, Intent intent) {

//对比Action决定输出什么信息

if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){

Log.e("HelloBroadReciever", "BOOT_COMPLETED !!!!!!!!!!!!!!!!!!!!!!!!!");

}



if(intent.getAction().equals("android.basic.lesson21.Hello")){

Log.e("HelloBroadReciever", "Say Hello to Yaoyao !!!!!!!!!!!!!!!!!!!!!!!!!");

Log.e("HelloBroadReciever", intent.getStringExtra("yaoyao"));

}



//播放一首音乐

MediaPlayer.create(context, R.raw.babayetu).start();

}

}
复制代码
       7、更改 AndroidManifest.xml 内容如下:

java代码:
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="" android:versionname="1.0" android:versioncode="1">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:label="@string/app_name" android:name=".MainBroadcastReceiver">

<intent -filter="">

<action android:name="android.intent.action.MAIN">

<category android:name="android.intent.category.LAUNCHER">

</category></action></intent>

</activity>

<!-- 定义Broadcast Receiver 指定监听的Action 这里我们的接收器,接收了2个Action,一个系统的一个我们自定义的 -->

<receiver android:name="HelloBroadReciever">

<intent -filter="">

<action android:name="android.intent.action.BOOT_COMPLETED">

</action></intent>

<intent -filter="">

<action android:name="android.basic.lesson21.HelloYaoYao">

</action></intent>



</receiver>

</application>

<uses -sdk="" android:minsdkversion="8">

</uses></manifest>
复制代码
系列之Android 深入解析用户界面(一)的帖子链接http://www.eoeandroid.com/thread-103258-1-1.html
系列之Android 深入解析用户界面(二)的帖子链接http://www.eoeandroid.com/thread-103259-1-1.html
系列之Android 深入解析用户界面(三)的帖子链接http://www.eoeandroid.com/thread-103263-1-1.html
系列之Android 深入解析用户界面(四)的帖子链接http://www.eoeandroid.com/thread-103266-1-1.html
系列之Android 深入解析用户界面(五)的帖子链接http://www.eoeandroid.com/thread-103437-1-1.html
系列之Android 深入解析用户界面(六)的帖子链接http://www.eoeandroid.com/thread-103438-1-1.html
系列之Android 深入解析用户界面(七)的帖子链接http://www.eoeandroid.com/thread-103445-1-1.html
系列之Android 深入解析用户界面(八)的帖子链接http://www.eoeandroid.com/thread-103448-1-1.html
系列之Android 深入解析用户界面(九)的帖子链接http://www.eoeandroid.com/thread-105421-1-1.html
系列之Android 深入解析用户界面(十)的帖子链接http://www.eoeandroid.com/thread-105424-1-1.html
系列之Android 深入解析用户界面(十二)的帖子链接http://www.eoeandroid.com/thread-103461-1-1.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-4-29 07:26 , Processed in 0.408139 second(s), 46 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表