全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

本文实例讲述了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程序设计有所帮助。


# Android  # Service  # Notification  # 定时发送  # Android入门之Service的使用详解  # Android NotificationListenerService通知监听服务使用  # Android Google AutoService框架使用详解  # Android使用Service实现IPC通信的2种方式  # 说说在Android如何使用服务(Service)的方法  # Android使用Service实现简单音乐播放实例  # 浅谈Android中Service的注册方式及使用  # Android 通知使用权(NotificationListenerService)的使用  # Android Service功能使用示例代码  # 状态栏  # 管理类  # 是一个  # 进阶  # 这是  # 操作技巧  # 上午  # 相关内容  # 给我们  # 感兴趣  # 给大家  # 自定义  # 它可以  # 跳转  # 更多关于  # 来实现  # 它将  # 所述  # 程序设计  # 应用程序 


相关文章: 国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?  高端网站建设与定制开发一站式解决方案 中企动力  javascript中的try catch异常捕获机制用法分析  内网网站制作软件,内网的网站如何发布到外网?  如何高效完成独享虚拟主机建站?  c++怎么编写动态链接库dll_c++ __declspec(dllexport)导出与调用【方法】  家具网站制作软件,家具厂怎么跑业务?  如何规划企业建站流程的关键步骤?  建站一年半SEO优化实战指南:核心词挖掘与长尾流量提升策略  大连网站制作公司哪家好一点,大连买房网站哪个好?  天津个人网站制作公司,天津网约车驾驶员从业资格证官网?  如何解决VPS建站LNMP环境配置常见问题?  建站主机解析:虚拟主机配置与服务器选择指南  历史网站制作软件,华为如何找回被删除的网站?  清除minerd进程的简单方法  常州自助建站:操作简便模板丰富,企业个人快速搭建网站  如何快速搭建二级域名独立网站?  网站按钮制作软件,如何实现网页中按钮的自动点击?  武汉网站如何制作,黄黄高铁武穴北站途经哪些村庄?  高防服务器:AI智能防御DDoS攻击与数据安全保障  岳西云建站教程与模板下载_一站式快速建站系统操作指南  阿里云网站制作公司,阿里云快速搭建网站好用吗?  如何续费美橙建站之星域名及服务?  网站设计制作企业有哪些,抖音官网主页怎么设置?  如何快速使用云服务器搭建个人网站?  南阳网站制作公司推荐,小学电子版试卷去哪里找资源好?  如何通过可视化优化提升建站效果?  公司门户网站制作流程,华为官网怎么做?  香港服务器租用每月最低只需15元?  制作旅游网站html,怎样注册旅游网站?  如何用低价快速搭建高质量网站?  胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?  公众号网站制作网页,微信公众号怎么制作?  如何通过PHP快速构建高效问答网站功能?  如何选择CMS系统实现快速建站与SEO优化?  手机网站制作与建设方案,手机网站如何建设?  制作充值网站的软件,做人力招聘为什么要自己交端口钱?  建站之星安装步骤有哪些常见问题?  大连企业网站制作公司,大连2025企业社保缴费网上缴费流程?  如何通过西部建站助手安装IIS服务器?  在线制作视频的网站有哪些,电脑如何制作视频短片?  独立制作一个网站多少钱,建立网站需要花多少钱?  如何选择最佳自助建站系统?快速指南解析优劣  实惠建站价格推荐:2025年高性价比自助建站套餐解析  如何撰写建站申请书?关键要点有哪些?  网站制作壁纸教程视频,电脑壁纸网站?  香港服务器租用费用高吗?如何避免常见误区?  阿里云高弹*务器配置方案|支持分布式架构与多节点部署  建站之星CMS建站配置指南:模板选择与SEO优化技巧  如何设置并定期更换建站之星安全管理员密码? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。