当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程使用Service实现Notification定时发送功能示例

Android编程使用Service实现Notification定时发送功能示例

2019年07月24日  | 移动技术网IT编程  | 我要评论

陶铸夫人,安特罗波娃,爱唯侦查power

本文实例讲述了android编程使用service实现notification定时发送功能。分享给大家供大家参考,具体如下:

/**
 * 通过启动或停止服务来管理通知功能
 *
 * @description:
 * @author ldm
 * @date 2016-4-29 上午9:15:15
 */
public class notifycontrolactivity extends activity {
  private button notifystart;// 启动通知服务
  private button notifystop;// 停止通知服务
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.notifying_controller);
    initwidgets();
  }
  private void initwidgets() {
    notifystart = (button) findviewbyid(r.id.notifystart);
    notifystart.setonclicklistener(mstartlistener);
    notifystop = (button) findviewbyid(r.id.notifystop);
    notifystop.setonclicklistener(mstoplistener);
  }
  private onclicklistener mstartlistener = new onclicklistener() {
    public void onclick(view v) {
      // 启动notification对应service
      startservice(new intent(notifycontrolactivity.this,
          notifyingservice.class));
    }
  };
  private onclicklistener mstoplistener = new onclicklistener() {
    public void onclick(view v) {
      // 停止notification对应service
      stopservice(new intent(notifycontrolactivity.this,
          notifyingservice.class));
    }
  };
}

/**
 * 实现每5秒发一条状态栏通知的service
 *
 * @description:
 * @author ldm
 * @date 2016-4-29 上午9:16:20
 */
public class notifyingservice extends service {
  // 状态栏通知的管理类对象,负责发通知、清楚通知等
  private notificationmanager mnm;
  // 使用layout文件的对应id来作为通知的唯一识别
  private static int mood_notifications = r.layout.status_bar_notifications;
  /**
   * android给我们提供conditionvariable类,用于线程同步。提供了三个方法block()、open()、close()。 void
   * block() 阻塞当前线程,直到条件为open 。 void block(long timeout)阻塞当前线程,直到条件为open或超时
   * void open()释放所有阻塞的线程 void close() 将条件重置为close。
   */
  private conditionvariable mcondition;
  @override
  public void oncreate() {
    // 状态栏通知的管理类对象,负责发通知、清楚通知等
    mnm = (notificationmanager) getsystemservice(notification_service);
    // 启动一个新个线程执行任务,因service也是运行在主线程,不能用来执行耗时操作
    thread notifyingthread = new thread(null, mtask, "notifyingservice");
    mcondition = new conditionvariable(false);
    notifyingthread.start();
  }
  @override
  public void ondestroy() {
    // 取消通知功能
    mnm.cancel(mood_notifications);
    // 停止线程进一步生成通知
    mcondition.open();
  }
  /**
   * 生成通知的线程任务
   */
  private runnable mtask = new runnable() {
    public void run() {
      for (int i = 0; i < 4; ++i) {
        // 生成带stat_happy及status_bar_notifications_happy_message内容的通知
        shownotification(r.drawable.stat_happy,
            r.string.status_bar_notifications_happy_message);
        if (mcondition.block(5 * 1000))
          break;
        // 生成带stat_neutral及status_bar_notifications_ok_message内容的通知
        shownotification(r.drawable.stat_neutral,
            r.string.status_bar_notifications_ok_message);
        if (mcondition.block(5 * 1000))
          break;
        // 生成带stat_sad及status_bar_notifications_sad_message内容的通知
        shownotification(r.drawable.stat_sad,
            r.string.status_bar_notifications_sad_message);
        if (mcondition.block(5 * 1000))
          break;
      }
      // 完成通知功能,停止服务。
      notifyingservice.this.stopself();
    }
  };
  @override
  public ibinder onbind(intent intent) {
    return mbinder;
  }
  @suppresswarnings("deprecation")
  private void shownotification(int moodid, int textid) {
    // 自定义一条通知内容
    charsequence text = gettext(textid);
    // 当点击通知时通过pendingintent来执行指定页面跳转或取消通知栏等消息操作
    notification notification = new notification(moodid, null,
        system.currenttimemillis());
    pendingintent contentintent = pendingintent.getactivity(this, 0,
        new intent(this, notifycontrolactivity.class), 0);
    // 在此处设置在nority列表里的该norifycation得显示情况。
    notification.setlatesteventinfo(this,
        gettext(r.string.status_bar_notifications_mood_title), text,
        contentintent);
    /**
     * 注意,我们使用出来。incoming_message id 通知。它可以是任何整数,但我们使用 资源id字符串相关
     * 通知。它将永远是一个独特的号码在你的 应用程序。
     */
    mnm.notify(mood_notifications, notification);
  }
  // 这是接收来自客户端的交互的对象. see
  private final ibinder mbinder = new binder() {
    @override
    protected boolean ontransact(int code, parcel data, parcel reply,
        int flags) throws remoteexception {
      return super.ontransact(code, data, reply, flags);
    }
  };
}

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center_horizontal"
  android:orientation="vertical"
  android:padding="4dip" >
  <textview
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:paddingbottom="4dip"
    android:text="通过service来实现对notification的发送管理" />
  <button
    android:id="@+id/notifystart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="启动服务" >
    <requestfocus />
  </button>
  <button
    android:id="@+id/notifystop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="停止服务" >
  </button>
</linearlayout>

更多关于android相关内容感兴趣的读者可查看本站专题:《android基本组件用法总结》、《android视图view技巧总结》、《android资源操作技巧汇总》、《android操作json格式数据技巧总结》、《android开发入门与进阶教程》、《android编程之activity操作技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网