Android基础_通知(Notification)

通知(Notification)是Android系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏后可以看到通知的详细内容。通知可以在活动、广播、服务中创建。


一、通知创建的过程


1.我们需要NotificationManager来对通知进行管理,一般用getSystemService()方法来获取其对象。

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


2.然后就是创建Notification的对象。

Notification notification = new Notification(R.drawable.icon, "This is ticker text",System.currentTimeMillis());

参数一:通知栏的图标

参数二:指定通知的提示内容

参数三:设置通知被创建的时间


3.设置通知的布局

标准布局setLatestEventInfo();

notification.setLatestEventInfo(context, "This is content title", "This is content text", null);

参数一:context

参数二:通知的标题

参数三:通知的具体内容

参数四:点击通知的响应,为null表示无响应

以后会学到自定义布局


4.显示通知

manager.notify(1,notification);

参数一:通知的id,方便NotificationManager对通知进行管理

参数二:创建的通知


二、设置通知点击后的响应(PendingIntent)

Intent intent = new Intent(this, NotificationActivity.class);

PendingIntent pi = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);

notification.setLatestEventInfo(this, "This is content title", "This is content text", pi);


三、一个简单的例子

MainActivity.java

package com.myNotification;

import java.io.File;
import com.example.mytest_notification.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
	
	Button open, close;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		initObjects();
	}
	
	private void initObjects(){
		open = (Button) findViewById(R.id.xml_open);
		close = (Button) findViewById(R.id.xml_close);
		open.setOnClickListener(this);
		close.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.xml_open:
				openNotification();
				break;
			case R.id.xml_close:
				closeNotification();
				break;
			default:
				break;
		}
	}
	
	@SuppressLint("SdCardPath") @SuppressWarnings("deprecation")
	private void openNotification(){
		NotificationManager manage = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.ic_launcher, "戚东亮看看", System.currentTimeMillis());
		Intent intent = new Intent(this, NotificationActivity.class);
		PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		notification.setLatestEventInfo(this, "标题", "具体内容", pi);
		Uri soundUri = Uri.fromFile(new File("/sdcard/Music/You Are Beautiful.mp3"));
		notification.sound = soundUri;
		long[] vibrates = {0, 500, 500, 500};
		notification.vibrate = vibrates;
		manage.notify(1, notification);
	}
	
	private void closeNotification(){
		NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		manager.cancelAll();
	}

}



NotificationActivity.java


package com.myNotification;

import com.example.mytest_notification.R;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

public class NotificationActivity extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification_layout);
		NotificationManager manage = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		manage.cancel(1);
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值