android的Notifications的例子demo

android的Notifications通知的原理和Demo

  在APP中经常会用到通知。这是个比较普遍的功能。比如网易新闻客户端,有什么重大新闻的话会在通知栏弹出一条通知。

  在做程序过程中我也遇到这个需求。每隔7天就自动弹出通知,提醒用户。在网上搜了搜,用了2天时间实现了。实现过程如下:

  一:通知要调用闹钟功能来实现,第一步设置闹钟

  /*参数1:context 参数2:唤醒的时间(毫秒格式)

   *功能:发出闹钟广播

  public static void setAlarmTime(Context context, long timeInMillis) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent("android.alarm.demo.action");
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
    int interval = 7*24*60*60*1000; //7天时间的毫秒形式
    am.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis,

    interval, sender);//参数2表示唤醒时间,参数2表示时间间隔。

  }

  要在需要的地方调用该方法,调用之后将调用时的时间存储起来,为什么,下面会说。

  存储代码:

  SharedPreferences alarmTime = context.getSharedPreferences(Constant.ALARM_TIME, 0);
  Editor editor = alarmTime.edit();
  editor.putLong("theFirstNotifyBeginTime",System.currentTimeMillis());
  editor.commit();

  二:接收步骤一中发出的BroadCast

  public class AlarmReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
  if ("android.alarm.demo.action".equals(intent.getAction())){

  //就在这里调用通知的操作
    NotifyUtil.addAppNotify(context); //我自己的通知的函数,见第三步

    SharedPreferences alarmTime = context.getSharedPreferences("alarm_time", 0);//为什么会有这个,接着往下看就行。
    Editor editor = alarmTime.edit();
    editor.putLong("lastNotifyTime",System.currentTimeMillis());
    editor.commit();
    return;
  }
}

}

  三:步骤二中要调用的通知函数。

  public class NotifyUtil {

    public static void addAppNotify(Context context){
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification();
    notification.icon = R.drawable.icon_app_notify; //通知图标
    notification.tickerText = context.getResources().getString(R.string.app_tickerText); //通知的内容
    notification.defaults=Notification.DEFAULT_SOUND; //通知的铃声
    notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    Intent intent = new Intent();
    intent.setComponent(new ComponentName("carman.execise","carman.execise.Main"));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    // 点击状态栏的图标出现的提示信息设置
    notification.setLatestEventInfo(context,context.getResources().getString(R.string.app_notify_title),
    context.getResources().getString(R.string.app_notify), pendingIntent);
    manager.notify(1, notification); //这个函数中第一个参数代表identifier.如果要同时弹出多条通知,每个通知的这个参数必须不同。

                   否则,后面的会覆盖前面的通知。
}
}

  以上步骤就可以实现通知功能,但是还缺一个重启的监听。否则,手机一重启所设置的闹钟就失效了。

  四:重启恢复闹钟,通过一个广播来实现

  因为重启之后所设置的闹钟就失效了,必须通过该广播重新计算好下次响的时间nextNotifyTime,并再次调用步骤一的方法。

  public class BootReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub
    String action = intent.getAction();
    if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
    SharedPreferences alarmTime = context.getSharedPreferences("alarm_time", 0);  //存储数据的首选项
    long lastNotifyTime = alarmTime.getLong("lastNotifyTime",0);  //取得上次闹钟响的时间,也就是步骤二中存储的值
    long nextNotifyTime;
    if(lastNotifyTime == 0){
    long theFirstNotifyBeginTime = alarmTime.getLong("theFirstNotifyBeginTime",0); //若没弹出过通知,取得步骤一中存储的值
    nextNotifyTime = 7*24*60*60*1000 + theFirstNotifyBeginTime;
    }else{
    nextNotifyTime = 7*24*60*60*1000 + lastNotifyTime;
    }
    if(nextNotifyTime <= System.currentTimeMillis()){
    NotifyUtil.setAlarmTime(context,System.currentTimeMillis());
    }else{
    NotifyUtil.setAlarmTime(context,nextNotifyTime); //再次设置为闹钟
    }
  }
  }
  }

  五:这些做好了之后调试还是通不过,因为需要在androidManifest中注册下广播的接收器才行。如下:

  步骤二的接收器,监听闹钟

  <receiver android:name=".notifications.AlarmReceiver">
    <intent-filter>
      <action android:name="android.alarm.demo.action" />
  </intent-filter>
  </receiver>

  步骤四的接收器,监听重启
  <receiver android:name=".notifications.BootReceiver">
    <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
  </receiver>

  黄色字体部分为闹钟重启监听的相关代码。

  可以转载,但请注明出处,谢谢!

  作者:Carman  2012-08-01  20:10:57

  邮箱:carman_loneliness@163.com

  需要源码可"评论"留下邮箱。

  

 

posted @ 2012-08-01 20:13  Andy Zhai  阅读(3763)  评论(16编辑  收藏  举报