您的位置:首页 > 移动开发 > Android开发

Android Notification用法

2014-09-16 14:25 141 查看
package com.example.notificationtest;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class NotificationTestActivity extends Activity {

	static final int NOTIFICATION_ID = 0x123;
	NotificationManager nm;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notification_test);
		
		nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.notification_test, menu);
		return true;
	}
	
	public void send(View Source)
	{
		Intent intent = new Intent(NotificationTestActivity.this,
				OtherActivity.class);
		PendingIntent pi = PendingIntent.getActivity(NotificationTestActivity.this, 0, intent, 0);
		Notification notify = new Notification.Builder(this)
		.setAutoCancel(true)
		.setTicker("有新消息")
		.setSmallIcon(R.drawable.ic_launcher)
		.setContentTitle("一条新通知")
		.setContentText("恭喜你,您加薪了!")
		.setWhen(System.currentTimeMillis())
		.setContentIntent(pi)
		.build();
		
		nm.notify(NOTIFICATION_ID,notify);
	}

	public void del(View v)
	{
		nm.cancel(NOTIFICATION_ID);
	}
	
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: