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

作者:迟做总比不做强 时间:2023-03-12 09:54:44 

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

来源:http://blog.csdn.net/true100/article/details/51279395

标签:Android,Service,Notification
0
投稿

猜你喜欢

  • Java实现指定线程执行顺序的三种方式示例

    2021-08-16 15:11:34
  • Mybatis一对多关联关系映射实现过程解析

    2021-07-13 06:22:59
  • Java全面细致讲解==和equals的使用

    2021-08-20 05:28:43
  • Android自定义View实现圆环进度条

    2023-09-20 00:19:13
  • c# 通过内存映射实现文件共享内存的示例代码

    2023-03-02 12:15:35
  • HttpClient 请求 URL字符集转码问题

    2022-11-20 17:33:07
  • spring boot中使用@Async实现异步调用任务

    2023-04-25 13:35:18
  • 优化SimpleAdapter适配器加载效率的方法

    2022-03-10 20:33:32
  • 详解Glide4.0集成及使用注意事项

    2021-12-28 00:09:06
  • C#中的DataSet、string、DataTable、对象转换成Json的实现代码

    2021-12-31 14:35:55
  • 解析Java继承中方法的覆盖和重载

    2021-09-02 02:02:32
  • JavaWeb使用Session和Cookie实现登录认证

    2023-12-11 19:13:29
  • SpringBoot集成支付宝沙箱支付(支付、退款)

    2022-02-15 16:50:52
  • JavaWeb中的常用的请求传参注解说明

    2023-06-19 03:12:06
  • Android ListView ImageView实现单选按钮实例

    2023-09-19 20:25:39
  • Unity实现滑动更换界面效果

    2021-10-06 19:56:40
  • java 算法之快速排序实现代码

    2023-01-30 01:44:59
  • Android自定义弹窗提示效果

    2022-05-13 12:00:14
  • 利用OPENCV为android开发畸变校正的JNI库方法

    2021-10-06 17:33:27
  • JAVA垃圾收集器与内存分配策略详解

    2023-05-20 16:33:20
  • asp之家 软件编程 m.aspxhome.com