最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程使用Service實(shí)現(xiàn)Notification定時(shí)發(fā)送功能示例

 更新時(shí)間:2017年08月19日 10:27:46   作者:遲做總比不做強(qiáng)  
這篇文章主要介紹了Android編程使用Service實(shí)現(xiàn)Notification定時(shí)發(fā)送功能,涉及Android服務(wù)Service控制通知的發(fā)送功能相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程使用Service實(shí)現(xiàn)Notification定時(shí)發(fā)送功能。分享給大家供大家參考,具體如下:

/**
 * 通過(guò)啟動(dòng)或停止服務(wù)來(lái)管理通知功能
 *
 * @description:
 * @author ldm
 * @date 2016-4-29 上午9:15:15
 */
public class NotifyControlActivity extends Activity {
  private Button notifyStart;// 啟動(dòng)通知服務(wù)
  private Button notifyStop;// 停止通知服務(wù)
  @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) {
      // 啟動(dòng)Notification對(duì)應(yīng)Service
      startService(new Intent(NotifyControlActivity.this,
          NotifyingService.class));
    }
  };
  private OnClickListener mStopListener = new OnClickListener() {
    public void onClick(View v) {
      // 停止Notification對(duì)應(yīng)Service
      stopService(new Intent(NotifyControlActivity.this,
          NotifyingService.class));
    }
  };
}

/**
 * 實(shí)現(xiàn)每5秒發(fā)一條狀態(tài)欄通知的Service
 *
 * @description:
 * @author ldm
 * @date 2016-4-29 上午9:16:20
 */
public class NotifyingService extends Service {
  // 狀態(tài)欄通知的管理類對(duì)象,負(fù)責(zé)發(fā)通知、清楚通知等
  private NotificationManager mNM;
  // 使用Layout文件的對(duì)應(yīng)ID來(lái)作為通知的唯一識(shí)別
  private static int MOOD_NOTIFICATIONS = R.layout.status_bar_notifications;
  /**
   * Android給我們提供ConditionVariable類,用于線程同步。提供了三個(gè)方法block()、open()、close()。 void
   * block() 阻塞當(dāng)前線程,直到條件為open 。 void block(long timeout)阻塞當(dāng)前線程,直到條件為open或超時(shí)
   * void open()釋放所有阻塞的線程 void close() 將條件重置為close。
   */
  private ConditionVariable mCondition;
  @Override
  public void onCreate() {
    // 狀態(tài)欄通知的管理類對(duì)象,負(fù)責(zé)發(fā)通知、清楚通知等
    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // 啟動(dòng)一個(gè)新個(gè)線程執(zhí)行任務(wù),因Service也是運(yùn)行在主線程,不能用來(lái)執(zhí)行耗時(shí)操作
    Thread notifyingThread = new Thread(null, mTask, "NotifyingService");
    mCondition = new ConditionVariable(false);
    notifyingThread.start();
  }
  @Override
  public void onDestroy() {
    // 取消通知功能
    mNM.cancel(MOOD_NOTIFICATIONS);
    // 停止線程進(jìn)一步生成通知
    mCondition.open();
  }
  /**
   * 生成通知的線程任務(wù)
   */
  private Runnable mTask = new Runnable() {
    public void run() {
      for (int i = 0; i < 4; ++i) {
        // 生成帶stat_happy及status_bar_notifications_happy_message內(nèi)容的通知
        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內(nèi)容的通知
        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內(nèi)容的通知
        showNotification(R.drawable.stat_sad,
            R.string.status_bar_notifications_sad_message);
        if (mCondition.block(5 * 1000))
          break;
      }
      // 完成通知功能,停止服務(wù)。
      NotifyingService.this.stopSelf();
    }
  };
  @Override
  public IBinder onBind(Intent intent) {
    return mBinder;
  }
  @SuppressWarnings("deprecation")
  private void showNotification(int moodId, int textId) {
    // 自定義一條通知內(nèi)容
    CharSequence text = getText(textId);
    // 當(dāng)點(diǎn)擊通知時(shí)通過(guò)PendingIntent來(lái)執(zhí)行指定頁(yè)面跳轉(zhuǎn)或取消通知欄等消息操作
    Notification notification = new Notification(moodId, null,
        System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, NotifyControlActivity.class), 0);
    // 在此處設(shè)置在nority列表里的該norifycation得顯示情況。
    notification.setLatestEventInfo(this,
        getText(R.string.status_bar_notifications_mood_title), text,
        contentIntent);
    /**
     * 注意,我們使用出來(lái)。incoming_message ID 通知。它可以是任何整數(shù),但我們使用 資源id字符串相關(guān)
     * 通知。它將永遠(yuǎn)是一個(gè)獨(dú)特的號(hào)碼在你的 應(yīng)用程序。
     */
    mNM.notify(MOOD_NOTIFICATIONS, notification);
  }
  // 這是接收來(lái)自客戶端的交互的對(duì)象. 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="通過(guò)Service來(lái)實(shí)現(xiàn)對(duì)Notification的發(fā)送管理" />
  <Button
    android:id="@+id/notifyStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="啟動(dòng)服務(wù)" >
    <requestFocus />
  </Button>
  <Button
    android:id="@+id/notifyStop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="停止服務(wù)" >
  </Button>
</LinearLayout>

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android資源操作技巧匯總》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

开封市| 积石山| 镇赉县| 嘉峪关市| 界首市| 石柱| 开鲁县| 宜良县| 木里| 镇原县| 宣汉县| 随州市| 铜陵市| 阿鲁科尔沁旗| 崇左市| 那曲县| 蓬溪县| 吉木乃县| 宜昌市| 馆陶县| 霍林郭勒市| 呈贡县| 五常市| 中卫市| 利津县| 承德市| 福清市| 樟树市| 渝北区| 来宾市| 白城市| 自贡市| 岳阳市| 高碑店市| 武功县| 和龙市| 松原市| 句容市| 繁峙县| 射洪县| 淮滨县|