欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

/**
 * 實現(xiàn)每5秒發(fā)一條狀態(tài)欄通知的Service
 *
 * @description:
 * @author ldm
 * @date 2016-4-29 上午9:16:20
 */
public class NotifyingService extends Service {
  // 狀態(tài)欄通知的管理類對象,負(fù)責(zé)發(fā)通知、清楚通知等
  private NotificationManager mNM;
  // 使用Layout文件的對應(yīng)ID來作為通知的唯一識別
  private static int MOOD_NOTIFICATIONS = R.layout.status_bar_notifications;
  /**
   * Android給我們提供ConditionVariable類,用于線程同步。提供了三個方法block()、open()、close()。 void
   * block() 阻塞當(dāng)前線程,直到條件為open 。 void block(long timeout)阻塞當(dāng)前線程,直到條件為open或超時
   * void open()釋放所有阻塞的線程 void close() 將條件重置為close。
   */
  private ConditionVariable mCondition;
  @Override
  public void onCreate() {
    // 狀態(tài)欄通知的管理類對象,負(fù)責(zé)發(fā)通知、清楚通知等
    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // 啟動一個新個線程執(zhí)行任務(wù),因Service也是運行在主線程,不能用來執(zhí)行耗時操作
    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)點擊通知時通過PendingIntent來執(zhí)行指定頁面跳轉(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);
    /**
     * 注意,我們使用出來。incoming_message ID 通知。它可以是任何整數(shù),但我們使用 資源id字符串相關(guān)
     * 通知。它將永遠(yuǎn)是一個獨特的號碼在你的 應(yīng)用程序。
     */
    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來實現(xiàn)對Notification的發(fā)送管理" />
  <Button
    android:id="@+id/notifyStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="啟動服務(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開發(fā)入門與進(jìn)階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》及《Android控件用法總結(jié)

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

相關(guān)文章

最新評論