码迷,mamicode.com
首页 > 移动开发 > 详细

android的通知栏的实现

时间:2017-04-16 22:56:34      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:toc   option   service   options   system   androi   when   this   protect   

package com.example.mynotification;

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.support.v4.app.NotificationCompat;
import android.view.Menu;

public class MainActivity extends Activity {
	
	public PendingIntent getDefalutIntent(int flags){
		PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);  
	    return pendingIntent;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
		mBuilder.setContentTitle("測试标题")//设置通知栏标题
				.setContentText("測试内容")//设置通知栏显示内容
				.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))//设置通知栏点击意图
			   //.setNumber(number);
				.setTicker("測试通知来啦")//通知栏首次出如今通知栏,带上动画效果
				.setWhen(System.currentTimeMillis())//通知栏时间。通常是直接用系统的
				.setPriority(Notification.DEFAULT_ALL)//设置通知栏优先级
			//	.setAutoCancel(true)//用户单击面板后消失
				.setOngoing(false)//true,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极參与(如播放音乐)或以某种方式正在等待,因此
				//占用设备(如一个文件下载,同步操作。主动网络连接)
				.setDefaults(Notification.DEFAULT_VIBRATE)//向通知加入声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置。
				//使用default属性。能够组合  
				//Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 加入声音 // requires VIBRATE permission  
				.setSmallIcon(R.drawable.ic_launcher);
		Notification notification = mBuilder.build();
		notification.flags = Notification.FLAG_ONGOING_EVENT  ;
		notification.flags = Notification.FLAG_NO_CLEAR;//点击清除的时候不清除
		Intent intent = new Intent(getApplicationContext(),MainActivity.class);	
		intent.addCategory(Intent.CATEGORY_LAUNCHER);   
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);  
		PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
		mBuilder.setContentIntent(pendingIntent);
		mNotificationManager.notify(0,<span style="font-family: Arial, Helvetica, sans-serif;">notification</span><span style="font-family: Arial, Helvetica, sans-serif;">);  </span>
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
不知道为什么我的点击还是会清除。郁闷。

android的通知栏的实现

标签:toc   option   service   options   system   androi   when   this   protect   

原文地址:http://www.cnblogs.com/yutingliuyl/p/6720056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!