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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值