Android 广播的用法示例

广播的作用这里就不废话了,本着代码为王的原则,直接上代码,比废话好。

我们先看下流程:

我们点击按钮,发送广播,然后广播接收器收到后,启动一个Alarm。

MainActivity的源码

[java]  view plain  copy
  1. package com.yongchun.intent.ui;  
  2. import java.util.Calendar;  
  3.   
  4. import android.app.Activity;  
  5. import android.app.AlarmManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. /*关于Intent和PendingIntent的区别 
  12.  * 一、Intent 
  13.  * 通常Android中的Intent位于     android.content.Intent的实现比较简单,直接从Object类实现, 
  14.  *内部主要是保存了一些String或Int、轻量级的数组,提供了一些方法主要是赋值或取值。 
  15.  * 
  16.  *二、PendingIntent 
  17.  *这里和Intent的不同分在了android.app.PendingIntent这个包中, 
  18.  *属于app层而不是数据存储封装的content层,从首段我们看到了PendingIntent是针对将要发生的事情, 
  19.  *比如短信发送时,本对象用于跟踪未来短信的接收情况,主要是短信回执报告和发送成功或失败, 
  20.  *因为GSM通讯到RIL再到移动基站的过程很漫长,通过开一个Thread等待对于我们的应用是比较麻烦和耗资源, 
  21.  *而Android的框架层的TelephonyManager底层远程服务会跟踪,最终通过PendingIntent来跟踪。 
  22.  */  
  23. public class MainActivity extends Activity implements OnClickListener {  
  24.     /** Called when the activity is first created. */  
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.         findViewById(R.id.send).setOnClickListener(this);  
  30.     }  
  31.   
  32.     public void onClick(View v) {  
  33.         // TODO Auto-generated method stub  
  34.         switch (v.getId()) {  
  35.         case R.id.send:  
  36.             setAlarm(true);  
  37.             break;  
  38.   
  39.         default:  
  40.             break;  
  41.         }  
  42.   
  43.     }  
  44.   
  45.     private void setAlarm(boolean bool) {  
  46.         /* 
  47.          * AlarmManager 全局定时器 
  48.          * 其用法跟Timer有点类似,作用有如下两点: 
  49.          * 第一:在指定时长后执行某项操作; 
  50.          * 第二:周期性的执行某项操作. 
  51.          * AlarmManager与Intent的配合使用,可以启动Activity,发送Broadcast,开启Service. 
  52.          * */  
  53.         AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);  
  54.   
  55.         PendingIntent pi = PendingIntent.getBroadcast(this0new Intent(  
  56.                 MainActivity.this, Receiver.class), 0);  
  57.   
  58.         if (bool) {  
  59.             Calendar c = Calendar.getInstance();  
  60.             am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);  
  61.         } else {  
  62.             am.cancel(pi);  
  63.         }  
  64.     }  
  65. }  

代码开始处有一大段的注释,详细的介绍了Intent和PendingIntent的区别,这里不再废话。

对应MainActivity的xml布局文件

main.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/send"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="点击发送广播" />  
  17.   
  18. </LinearLayout>  

广播接收器的源码

Receiver.Java

[java]  view plain  copy
  1. package com.yongchun.intent.ui;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6.   
  7. /** 
  8.  * 作者:肥鱼 QQ群:104780991 Email:zhaoyongchun2011@gmail.com 
  9.  * 关于:一条致力于Android开源事业的鱼,还是肥的.吃得多赚的少还不会暖床,求包养. 
  10.  */  
  11. public class Receiver extends BroadcastReceiver {  
  12.   
  13.     @Override  
  14.     public void onReceive(Context context, Intent intent) {  
  15.         // TODO Auto-generated method stub  
  16.   
  17.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  18.         intent.setClass(context, Alarm.class);  
  19.         context.startActivity(intent);  
  20.     }  
  21.   
  22. }  

由广播接收器启动的Alarm的Activity的代码

Alarm.java

[java]  view plain  copy
  1. package com.yongchun.intent.ui;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.net.Uri;  
  7. import android.os.Bundle;  
  8. import android.provider.MediaStore.Audio;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14.   
  15. /** 
  16.  * 作者:肥鱼 QQ群:104780991 Email:zhaoyongchun2011@gmail.com 
  17.  * 关于:一条致力于Android开源事业的鱼,还是肥的.吃得多赚的少还不会暖床,求包养. 
  18.  */  
  19. public class Alarm extends Activity {  
  20.   
  21.     private Button cancelButton;  
  22.     private TextView show;  
  23.     private static int NOTIFICATION_ID = 1;  
  24.   
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         // TODO Auto-generated method stub  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.alarm);  
  30.   
  31.         final NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  32.   
  33.         Notification notification = new Notification();  
  34.   
  35.         // notification.sound = Uri.withAppendedPath(  
  36.         // Audio.Media.INTERNAL_CONTENT_URI, "20");  
  37.   
  38.         notification.sound = Uri.parse("file:///sdcard/alert.mp3");  
  39.   
  40.         nm.notify(NOTIFICATION_ID, notification);  
  41.   
  42.         show = (TextView) findViewById(R.id.showalarm);  
  43.         show.setText("时间到啦。。。。");  
  44.   
  45.         cancelButton = (Button) findViewById(R.id.cancel);  
  46.         cancelButton.setOnClickListener(new OnClickListener() {  
  47.   
  48.             public void onClick(View v) {  
  49.                 // TODO Auto-generated method stub  
  50.                 nm.cancel(NOTIFICATION_ID);  
  51.                 finish();  
  52.             }  
  53.         });  
  54.     }  
  55. }  

对应Alarm的xml布局文件为:

alarm.xml

[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/linearLayout1"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/showalarm"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="Large Text"  
  12.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  13.   
  14.     <Button  
  15.         android:id="@+id/cancel"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="取消" />  
  19.   
  20. </LinearLayout>  

最后,要在AndroidManifest文件中注册一下Activity和Receiver

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.yongchun.intent.ui"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:name=".MainActivity"  
  14.             android:label="@string/app_name" >  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.         <activity android:name=".Alarm" >  
  22.         </activity>  
  23.   
  24.         <receiver android:name=".Receiver" >  
  25.             <intent-filter>  
  26.                 <action android:name="com.yongchun.intent.ui.Receiver" />  
  27.             </intent-filter>  
  28.         </receiver>  
  29.     </application>  
  30.   
  31. </manifest>  

点击下载示例代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值