Android 如何在没有图标但启用了led的情况下发布通知?
大家好,Android程序员们 我目前正在编写一个应用程序,使用新的Android 4.3通知侦听器在收到通知时更改通知led的颜色,我面临一个问题,可能是因为我不了解通知的工作原理 到目前为止,它运行得相当好。当用户关闭屏幕时,我会创建一个带有自定义led颜色的通知,并在屏幕打开时将其删除。我的问题是,当用户重新打开屏幕时,他可以在状态栏上看到我的通知图标半秒钟,然后将其删除。没什么大不了的,但作为一个吹毛求疵的人,我忍不住想办法避免这种丑恶的行为。我知道有些应用程序在这方面取得了成功——例如LightFlow 我的第一个想法是利用通知的优先级并使用通知。priority_MIN几乎成功了:通知图标不会显示在状态栏上,而是在状态栏展开时出现 我不幸地发现,当屏幕关闭时,具有最低优先级的通知不会切换通知指示灯 然后我尝试创建一个没有图标的通知,但是框架不支持它——这实际上是一件好事 现在我不知道了 有人能帮我找到一种方法来创建一个通知,该通知不会出现在状态栏上,但仍会打开LED 或者我应该在屏幕真正打开之前删除我的通知,但我也找不到这样做的方法 如果有人能在这件事上帮我,我会很高兴的 以下是我的应用程序的代码源: 主要活动: 服务侦听器 屏幕状态接收器: 提前谢谢你Android 如何在没有图标但启用了led的情况下发布通知?,android,android-notifications,android-notification-bar,Android,Android Notifications,Android Notification Bar,大家好,Android程序员们 我目前正在编写一个应用程序,使用新的Android 4.3通知侦听器在收到通知时更改通知led的颜色,我面临一个问题,可能是因为我不了解通知的工作原理 到目前为止,它运行得相当好。当用户关闭屏幕时,我会创建一个带有自定义led颜色的通知,并在屏幕打开时将其删除。我的问题是,当用户重新打开屏幕时,他可以在状态栏上看到我的通知图标半秒钟,然后将其删除。没什么大不了的,但作为一个吹毛求疵的人,我忍不住想办法避免这种丑恶的行为。我知道有些应用程序在这方面取得了成功——例如
埃里克。我可以问你最终是如何做到这一点的吗?我正在尝试为我的应用程序实现类似的功能。不幸的是,我从未实现过。
package com.nightlycommit.coloration;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
package com.nightlycommit.coloration;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.service.notification.StatusBarNotification;
/**
* Created by Eric on 08/08/13.
*/
public class NotificationListenerService extends android.service.notification.NotificationListenerService {
private final String TAG = getClass().getSimpleName();
@Override
public void onCreate() {
super.onCreate();
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenStateReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
String text = "Notification posted !";
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
String text = "Notification removed !";
}
}
package com.nightlycommit.coloration;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
/**
* Created by Eric on 12/08/13.
*/
public class ScreenStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
String action = intent.getAction();
if (action != null) {
if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Notification.Builder builder = new Notification.Builder(context);
builder.setLights(Color.argb(255,255,0,0), 500, 500);
builder.setContentText("TEST");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setOngoing(true);
// builder.setPriority(Notification.PRIORITY_MIN);
notificationManager.notify(777, builder.build());
} else if (action.equals(Intent.ACTION_SCREEN_ON)) {
notificationManager.cancel(777);
}
}
}
}