Notification(通知)

安卓手机的app有着各式各样的通知,而这些通知又是如何实现的呢?

今天就分享一下通知的使用

如果我们想让通知开机的时候就启动,就需要使用到BroadcastReceiver

package com.example.notificationdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class PowerUp extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		//开机就调用show方法
		NotificationUtil.show(context);
	}

}
开机启动的广播为:

        <receiver 
            android:name="com.example.notificationdemo.PowerUp">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
当然,开机启动也是需要权限的:

<!--开机启动  -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

package com.example.notificationdemo;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

/**
 * 发送通知
 * 
 */
public class NotificationUtil {

	public static void show(Context context) {
		// 1.获取NotificationManager对象,属于框架层
		NotificationManager manager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		
		Notification notification = new Notification();
		/**
		 * DEFAULT_ALL-->下面三种都会有
		 * DEFAULT_LIGHTS-->呼吸灯会闪烁
		 * DEFAULT_SOUND-->提示音
		 * DEFAULT_VIBRATE-->震动 震动需要添加权限
		 */
		notification.defaults = Notification.DEFAULT_ALL;
		// notification.ledARGB :呼吸灯的颜色
		// notification.ledOffMS:消失的时间
		// notification.ledOnMS:开始的时间
		// notification.sound:通过uri设置

		/**
		 * FLAG_AUTO_CANCEL-->点击到此通知时自动取消 FLAG_FOREGROUND_SERVICE-->前置服务(暂时没用)
		 * FLAG_INSISTENT-->没有取消会以大图标的形式显示 FLAG_NO_CLEAR-->没有办法取消
		 * FLAG_ONLY_ALERT_ONCE-->只提醒一次 FLAG_SHOW_LIGHTS-->呼吸灯会显示
		 * FLAG_ONGOING_EVENT-->比如电话没接到会受到提示
		 */
		notification.flags = Notification.FLAG_NO_CLEAR;

		/**
		 * 点击通知之后的意图
		 */
		Intent intent = new Intent(context, MainActivity.class);
		// 延时意图,处理即将发生的事情
		PendingIntent pendingIntent = PendingIntent.getActivity(context,
				0/* 请求码 */, intent/* 点击通知要跳转的意图 */,
				PendingIntent.FLAG_UPDATE_CURRENT);
		notification.contentIntent = pendingIntent;
		
		/**
		 * 设置样式
		 */
		notification.icon = R.id.action_settings;//图标
		notification.tickerText = "标题";
		//简易的通知信息,已经过时
//		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent)
		//RemoteViews(通知的内容视图):参数1包名,参数2为自定义布局
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.activity_main);
		/**
		 * 获取视图的控件
		 * setOnClickPendingIntent: 限制:如果在listView就无效
		 * ListView的点击:要使用remoteViews.setOnClickFillInIntent(viewId, fillInIntent)
		 * fillInIntent:普通的Intent,会自动转换成延时意图
		 * 参数1:控件的id   参数2:意图
		 */
//		remoteViews.setOnClickFillInIntent(viewId, fillInIntent)
//		点击控件取消可以在广播或者服务中的声明周期调用取消的方法。
		remoteViews.setOnClickPendingIntent(R.id.action_settings, pendingIntent);
//		remoteViews.setTextViewText(viewId, text)//根据id改变控件TextView的文本内容,Button是TextView的子类,可以用
//		remoteViews.setImageViewResource(viewId, srcId)//改变前景图片
		//也可以设置适配器
		
		notification.contentView = remoteViews;
		
		// 发送通知信息
		/**
		 * tag-->标签,如果id相同可以通过tag取消 id-->可以通过id取消通知 notification-->要发送的消息
		 */
		// manager.notify(tag, id, notification)
		manager.notify(0, notification);
	}
	
	public static void cancle(Context context) {
		NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		manager.cancel(0);
	}
}

由于上面代码调用了震动的方法,而震动也需要权限

<!--震动权限  -->
    <uses-permission android:name="android.permission.VIBRATE"/>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值