国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程使用Service實現Notification定時發送功能示例

2019-10-22 18:29:49
字體:
來源:轉載
供稿:網友

本文實例講述了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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 壶关县| 永昌县| 荔浦县| 黄陵县| 辽阳县| 星子县| 平陆县| 冀州市| 兴安盟| 通山县| 霍山县| 南宁市| 静海县| 牙克石市| 青冈县| 蒙山县| 广丰县| 大余县| 高青县| 昆山市| 迁安市| 金秀| 卫辉市| 若羌县| 鄂托克前旗| 湛江市| 屯昌县| 云和县| 大冶市| 肃宁县| 冀州市| 吐鲁番市| 饶阳县| 四子王旗| 伊川县| 白沙| 梨树县| 攀枝花市| 成武县| 广东省| 乌审旗|