android功能说明,Android Notifaction使用说明

一般通知

以下三个方法必须调用

mBulider.setSmallIcon(R.drawable.notification_icon)

.setContentTitle("My notification")

.setContentText("Hello World!")

NotificationManager mNotifyMgr =

(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

PendingIntent contentIntent = PendingIntent.getActivity(

this, 0, new Intent(this, ResultActivity.class), 0);

NotificationCompat.Builder mBuilder =

new NotificationCompat.Builder(this)

.setSmallIcon(R.drawable.notification_icon)

.setContentTitle("My notification")

.setContentText("Hello World!")

.setContentIntent(contentIntent);

mNotifyMgr.notify(NOTIFICATIONS_ID, mBuilder.build());

进度条通知

明确进度的进度条

使用setProgress(max, progress, false)来更新进度。

max: 最大进度值

progress: 当前进度

false: 是否是不明确的进度条

int id = 1;

...

mNotifyManager = (NotificationManager)

getSystemService(Context.NOTIFICATION_SERVICE);

mBuilder = new NotificationCompat.Builder(this);

mBuilder.setContentTitle("Picture Download")

.setContentText("Download in progress")

.setSmallIcon(R.drawable.ic_notification);

// Start a lengthy operation in a background thread

new Thread(

new Runnable() {

@Override

public void run() {

int incr;

for (incr = 0; incr <= 100; incr+=5) {

mBuilder.setProgress(100, incr, false);

mNotifyManager.notify(id, mBuilder.build());

try {

// Sleep for 5 seconds

Thread.sleep(5*1000);

} catch (InterruptedException e) {

Log.d(TAG, "sleep failure");

}

}

mBuilder.setContentText("Download complete")//下载完成

.setProgress(0,0,false); //移除进度条

mNotifyManager.notify(id, mBuilder.build());

}

}

).start();

自定义通知

自定义普通视图通知高度限制为64dp,大视图通知高度限制为256dp。自定义通知需要做如下操作:

1、创建自定义通知布局

2、使用RemoteViews定义通知组件,如图标、文字等

3、调用setContent()将RemoteViews对象绑定到NotificationCompat.Builder

4、同正常发送通知流程

RemoteViews bigView;

RemoteViews smallView;

// 构建 bigView 和 smallView。

...

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

// 设置自定义 RemoteViews

builder.setContent(smallView).setSmallIcon(R.drawable.icon_notification);

Notification notification = builder.build();

// 如果系统版本 >= Android 4.1,设置大视图 RemoteViews

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

notification.bigContentView = bigView;

}

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(DAILY_PUSH_NOTIFICATION_ID, notification);

浮动通知

以下两种情况会显示浮动通知:

setFullScreenIntent()

NotificationCompat.Builder mNotifyBuilder =

new NotificationCompat.Builder(this)

.setContentTitle("New Message")

.setContentText("You've received new messages.")

.setSmallIcon(R.drawable.ic_notify_status)

.setFullScreenIntent(pendingIntent, false);

> - 需要设置 Notification 的优先级为 PRIORITY_HIGH 或者 PRIORITY_MAX 并且使用铃声或振动

// 设置通知的优先级

builder.setPriority(NotificationCompat.PRIORITY_MAX);

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

// 设置通知的提示音

builder.setSound(alarmSound);

// 设置通知的优先级

mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);

// Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

// // 设置通知的提示音

// mBuilder.setSound(alarmSound);

mBuilder.setDefaults(Notification.DEFAULT_ALL);

## 锁屏通知

> Android 5.0(API level 21)开始,通知可以显示在锁屏上。你的应用可以通过setVisibility()控制通知的显示等级

- VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容

- VISIBILITY_PUBLIC : 显示通知的全部内容

- VISIBILITY_SECRET : 不显示任何内容,包括图标

#⚠️ 注意

**经测试,针对浮动通知与锁屏通知,三星手机有效,华为手机在于手机通知中心设置,不在代码。**

> - 华为手机5.0系统与6.0系统 通知中心 可以设置通知方式,默认状态栏通知方式打开,横幅(浮动通知)方式关闭和锁屏(锁屏通知)关闭,则调用以上代码无效

- 三星手机调用有效

## 通知更新设置

> 1. 再次发送相同ID的通知,如果之前的通知依然存在则会更新通知属性,如果之前通知不存在则重新创建

2. 设置了 setAutoCancel(true) ,点击该通知时会清除它

3. 通过 NotificationManager 调用 cancel() 方法清除指定ID的通知

4. 通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知

5. 点击通知栏的清除按钮,会清除所有可清除的通知

## 通知效果设置

1. 对于铃声、闪光、震动这三个属性,NotificationCompat.Builder提供了三个方法设定:

- setSound(Uri sound):设定一个铃声,用于在通知的时候响应。传递一个Uri的参数,格式为“file:///mnt/sdcard/Xxx.mp3”。

- setLights(int argb, int onMs, int offMs):设定前置LED灯的闪烁速率,持续毫秒数,停顿毫秒数。

- setVibrate(long[] pattern):设定震动的模式,以一个long数组保存毫秒级间隔的震动。

2. 一般使用setDefaults(int)方法设定默认响应参数

- DEFAULT_ALL:铃声、闪光、震动均系统默认

- DEFAULT_SOUND:系统默认铃声

- DEFAULT_VIBRATE:系统默认震动

- DEFAULT_LIGHTS:系统默认闪光

3. 而在Android中,如果需要访问硬件设备的话,是需要对其进行授权的,所以需要在清单文件AndroidManifest.xml中增加两个授权,分别授予访问振动器与闪光灯的权限:

## Activity返回栈

### 常规Activity

> 默认情况下,从通知启动一个Activity,按返回键会回到主屏幕。但某些时候有按返回键仍然留在当前应用的需求,这就要用到TaskStackBuilder了。

1、在manifest中定义Activity的关系

Android 4.0.3 及更早版本

android:name=".ResultActivity">

android:name="android.support.PARENT_ACTIVITY"

android:value=".MainActivity"/>

Android 4.1 及更高版本

android:name=".ResultActivity"

android:parentActivityName=".MainActivity">

2、创建返回栈PendingIntent

Intent resultIntent = new Intent(this, ResultActivity.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// 添加返回栈

stackBuilder.addParentStack(ResultActivity.class);

// 添加Intent到栈顶

stackBuilder.addNextIntent(resultIntent);

// 创建包含返回栈的pendingIntent

PendingIntent resultPendingIntent =

stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setContentIntent(resultPendingIntent);

NotificationManager mNotificationManager =

(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(id, builder.build());

上述操作后,从通知启动ResultActivity,按返回键会回到MainActivity,而不是主屏幕。

### 特殊Activity

> 默认情况下,从通知启动的Activity会在近期任务列表里出现。如果不需要在近期任务里显示,则需要做以下操作:

1、在manifest中定义Activity

android:name=".ResultActivity"

android:launchMode="singleTask"

android:taskAffinity=""

android:excludeFromRecents="true">

2、构建PendingIntent

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

Intent notifyIntent = new Intent(this, ResultActivity.class);

// Sets the Activity to start in a new, empty task

notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK

| Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent notifyPendingIntent =

PendingIntent.getActivity(this, 0, notifyIntent,

PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(notifyPendingIntent);

NotificationManager mNotificationManager =

(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(id, builder.build());

上述操作后,从通知启动ResultActivity,此Activity不会出现在近期任务列表中。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值