广播的作用这里就不废话了,本着代码为王的原则,直接上代码,比废话好。
我们先看下流程:
我们点击按钮,发送广播,然后广播接收器收到后,启动一个Alarm。
MainActivity的源码
- package com.yongchun.intent.ui;
- import java.util.Calendar;
- import android.app.Activity;
- import android.app.AlarmManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- /*关于Intent和PendingIntent的区别
- * 一、Intent
- * 通常Android中的Intent位于 android.content.Intent的实现比较简单,直接从Object类实现,
- *内部主要是保存了一些String或Int、轻量级的数组,提供了一些方法主要是赋值或取值。
- *
- *二、PendingIntent
- *这里和Intent的不同分在了android.app.PendingIntent这个包中,
- *属于app层而不是数据存储封装的content层,从首段我们看到了PendingIntent是针对将要发生的事情,
- *比如短信发送时,本对象用于跟踪未来短信的接收情况,主要是短信回执报告和发送成功或失败,
- *因为GSM通讯到RIL再到移动基站的过程很漫长,通过开一个Thread等待对于我们的应用是比较麻烦和耗资源,
- *而Android的框架层的TelephonyManager底层远程服务会跟踪,最终通过PendingIntent来跟踪。
- */
- public class MainActivity extends Activity implements OnClickListener {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViewById(R.id.send).setOnClickListener(this);
- }
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch (v.getId()) {
- case R.id.send:
- setAlarm(true);
- break;
- default:
- break;
- }
- }
- private void setAlarm(boolean bool) {
- /*
- * AlarmManager 全局定时器
- * 其用法跟Timer有点类似,作用有如下两点:
- * 第一:在指定时长后执行某项操作;
- * 第二:周期性的执行某项操作.
- * AlarmManager与Intent的配合使用,可以启动Activity,发送Broadcast,开启Service.
- * */
- AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
- PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(
- MainActivity.this, Receiver.class), 0);
- if (bool) {
- Calendar c = Calendar.getInstance();
- am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
- } else {
- am.cancel(pi);
- }
- }
- }
代码开始处有一大段的注释,详细的介绍了Intent和PendingIntent的区别,这里不再废话。
对应MainActivity的xml布局文件
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button
- android:id="@+id/send"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="点击发送广播" />
- </LinearLayout>
广播接收器的源码
Receiver.Java
- package com.yongchun.intent.ui;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- /**
- * 作者:肥鱼 QQ群:104780991 Email:zhaoyongchun2011@gmail.com
- * 关于:一条致力于Android开源事业的鱼,还是肥的.吃得多赚的少还不会暖床,求包养.
- */
- public class Receiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setClass(context, Alarm.class);
- context.startActivity(intent);
- }
- }
由广播接收器启动的Alarm的Activity的代码
Alarm.java
- package com.yongchun.intent.ui;
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore.Audio;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- /**
- * 作者:肥鱼 QQ群:104780991 Email:zhaoyongchun2011@gmail.com
- * 关于:一条致力于Android开源事业的鱼,还是肥的.吃得多赚的少还不会暖床,求包养.
- */
- public class Alarm extends Activity {
- private Button cancelButton;
- private TextView show;
- private static int NOTIFICATION_ID = 1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.alarm);
- final NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- Notification notification = new Notification();
- // notification.sound = Uri.withAppendedPath(
- // Audio.Media.INTERNAL_CONTENT_URI, "20");
- notification.sound = Uri.parse("file:///sdcard/alert.mp3");
- nm.notify(NOTIFICATION_ID, notification);
- show = (TextView) findViewById(R.id.showalarm);
- show.setText("时间到啦。。。。");
- cancelButton = (Button) findViewById(R.id.cancel);
- cancelButton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- nm.cancel(NOTIFICATION_ID);
- finish();
- }
- });
- }
- }
对应Alarm的xml布局文件为:
alarm.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/linearLayout1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/showalarm"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Large Text"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- <Button
- android:id="@+id/cancel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="取消" />
- </LinearLayout>
最后,要在AndroidManifest文件中注册一下Activity和Receiver
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.yongchun.intent.ui"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="8" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".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>
- <activity android:name=".Alarm" >
- </activity>
- <receiver android:name=".Receiver" >
- <intent-filter>
- <action android:name="com.yongchun.intent.ui.Receiver" />
- </intent-filter>
- </receiver>
- </application>
- </manifest>
点击下载示例代码