Android Notification

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。