Notification(四)——Notification完整使用实例详解

MainActivity如下:

package cc.cv;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
/**
 * Demo描述:
 * Notification完整使用实例
 * 
 * 注意权限:
 * <uses-permission android:name="android.permission.VIBRATE"/>"
 * 
 * 
 * 参考资料:
 * 1 http://blog.csdn.net/vipzjyno1/article/details/25248021
 * 2 http://adchs.github.io
 *   Thank you very much
 */
public class MainActivity extends Activity {
	private Button mSendButton;
	private Button mUpdateButton;
	private Context mContext;
	private Builder mBuilder;
	private PendingIntent mPendingIntent;
    private Notification mNotification;
    private NotificationManager mNotificationManager;
    private final int NOTIFICATION_ID=9527;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	
	private void init(){
		mContext=this;
		mSendButton=(Button) findViewById(R.id.sendButton);
		mSendButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				sendNotification(mContext);
			}
		});
		mUpdateButton=(Button) findViewById(R.id.updateButton);
		mUpdateButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				updateNotification();
			}
		});
	}
	

	/**
	 * 发送通知
	 */
	private void sendNotification(Context context){
		Intent intent=new Intent(mContext, MainActivity.class);
		mPendingIntent=PendingIntent.getActivity(mContext, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
		mNotificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		mBuilder=new Builder(mContext);
		//通知产生的时间
		mBuilder.setWhen(System.currentTimeMillis());
		//通知首次出现在通知栏时的提示文字
		mBuilder.setTicker("Ticker");
		mBuilder.setContentTitle("ContentTitle");
		mBuilder.setContentInfo("ContentInfo");
		mBuilder.setContentText("ContentText");
		mBuilder.setContentIntent(mPendingIntent);
		//通知的优先级
		mBuilder.setPriority(Notification.PRIORITY_DEFAULT);
		//设置通知的图标.必须要有这句否则通知不显示!!!
		mBuilder.setSmallIcon(R.drawable.ic_launcher);
		//为通知添加声音,闪灯和振动效果等效果
		mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
		//设置通知是否为一个正在进行的通知.后台任务时常使用true
		mBuilder.setOngoing(false);
		
		mNotification=mBuilder.build();
		//通知被点击后自动消失
		mNotification.flags = Notification.FLAG_AUTO_CANCEL;
		mNotificationManager.notify(NOTIFICATION_ID, mNotification);
	}
	
	
	/**
	 * 更新通知
	 */
	private void updateNotification(){
		mBuilder.setContentTitle("Title");
		mBuilder.setContentInfo("Info");
		mBuilder.setContentText("Text");
		mNotification=mBuilder.build();
		mNotification.flags = Notification.FLAG_AUTO_CANCEL;
		mNotificationManager.notify(NOTIFICATION_ID, mNotification);
	}

}



main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/sendButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送通知" 
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dip" />
    
    
    <Button
        android:id="@+id/updateButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更新通知" 
        android:layout_centerHorizontal="true"
        android:layout_marginTop="250dip" />

</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值