栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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


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));
    }
  };
}


public class NotifyingService extends Service {
  // 状态栏通知的管理类对象,负责发通知、清楚通知等
  private NotificationManager mNM;
  // 使用Layout文件的对应ID来作为通知的唯一识别
  private static int MOOD_NOTIFICATIONS = R.layout.status_bar_notifications;
  
  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);
    
    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);
    }
  };
}



  
  
  


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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158088.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号