Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Notification

Android Notification

編輯:關於Android編程

package com.example.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends Activity
{

	private Handler handler = new Handler();
	private int mId = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		handler.post(runnable);
	}
	
	private Runnable runnable = new Runnable()
	{
		
		@Override
		public void run()
		{
			if(mId < 3)
			{
				sendNotification(mId);
				mId++;
			}
			
			handler.postDelayed(runnable, 5000);
		}
	};

	/**
	 * 根據不同的ID發送不同類型的通知
	 * 
	 * @param mId
	 */
	@SuppressWarnings("deprecation")
	protected void sendNotification(int mId)
	{
		NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		
		Notification noti = new Notification(R.drawable.ic_launcher, "狀態欄顯示滾輪信息", System.currentTimeMillis());
		
		noti.defaults |= Notification.DEFAULT_VIBRATE;
//		noti.vibrate = null;
		
		noti.defaults |= Notification.DEFAULT_SOUND;
//		noti.sound = null;
		
		noti.flags |= Notification.FLAG_AUTO_CANCEL;
		
		Intent intent = new Intent(MainActivity.this, ResultActivity.class);
		intent.putExtra("fromTag", "傳遞數據");
		PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		noti.setLatestEventInfo(MainActivity.this, "通知欄標題" + mId, "通知欄信息詳情" + mId, pendingIntent);
		
		notificationManager.notify(mId, noti);
		
	}

	@Override
	protected void onDestroy()
	{
		handler.removeCallbacks(runnable);
		super.onDestroy();
	}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved