使用Notification发送消息通知

使用Notification发送消息通知_第1张图片使用Notification发送消息通知_第2张图片使用Notification发送消息通知_第3张图片

demo代码:

MainActivity.java

package com.example.notificationdemo;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
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;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

	NotificationManager manager;//通知控制类
	int notification_ID = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		findViewById(R.id.btn_send).setOnClickListener(this);
		findViewById(R.id.btn_cancle).setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_send:
			sendNotification();
			break;
		case R.id.btn_cancle:
			manager.cancel(notification_ID);
			break;
		}
	}
	/**
	 * 构造notification并发送到通知栏
	 */

	private void sendNotification(){
		Intent intent = new Intent(this,MainActivity.class);
		PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);
		Builder builder = new Notification.Builder(this);
		builder.setSmallIcon(R.drawable.ic_launcher);//设置图标
		builder.setTicker("hello");//手机状态栏的提示;
		builder.setWhen(System.currentTimeMillis());//设置时间
		builder.setContentTitle("通知栏通知");//设置标题
		builder.setContentText("我来自NotificationDemo");//设置通知内容
		builder.setContentIntent(pintent);//点击后的意图
		builder.setDefaults(Notification.DEFAULT_SOUND);//设置提示声音(不要忘记权限设置)
		builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯(不要忘记权限设置)
//		builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动(不要忘记权限设置)
		builder.setDefaults(Notification.DEFAULT_ALL);//设置震动
//		Notification notification = builder.build();//4.1以上
		Notification notification = builder.getNotification();//4.1以下
		manager.notify(notification_ID, notification);
	}


}
AndroidManifest.xml



    
    
    
    
        
            
                

                
            
        
    




你可能感兴趣的:(android)