我只想启动和停止状态栏中的同步图标.我认为这将是一个使用NotificationManager的简单调用,但我无法在Web或SO上找到文档或示例问答.
我一直试图使用以下方法删除服务设置的持久通知:
startForeground(1337, notification);
Run Code Online (Sandbox Code Playgroud)
我用来取消它的代码:
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.cancel(1337); // cancel existing service notification, doesn't take effect
nManager.cancelAll(); //surpluous, but also doesn't take effect
Run Code Online (Sandbox Code Playgroud)
为了澄清我为什么这样做:服务以默认的持久性通知开始.当我的应用运行时,它需要将此通知替换为另一个.notify()
然而,使用现有的通知工作非常完美,我还需要它来显示新通知的滚动条文本.这就是为什么我决定删除现有的通知(使用上面的代码),创建一个新的,然后我startForeground()
再次调用并将新的通知传递给它,所以我的服务仍然存在.
android foreground android-service notificationmanager android-notifications
我一直在浏览NotificationManager
API级别10的android文档中的类,但还没有找到实现它的方法.我也浏览了谷歌,但只找到了如何设置通知实际设置的时间以及如何设置显示Toast通知小部件的时间长度.
我已经设置了FLAG_NO_CLEAR
并FLAG_ONGOING_EVENT
保持通知图标存在,并在展开通知下拉列表后保持通知可见.
我试图在通知栏中显示通知的文本10秒而不是标准时间.所以我基本上想要完成该setDuration(int)
功能在Toast小部件中的功能.
我想知道这是否可能,如果有的话,如果有人能指出我正确的方向如何实现它.
当我的应用程序下载文件时,我已经实现了包含动画的通知,它在Lollipop出现之前一直运行良好.
public void createNotification(String filename) {
mBuilder = new Notification.Builder(this);
mBuilder.setContentTitle(filename);
mBuilder.setContentText(getString(R.string.downloading));
mBuilder.setSmallIcon(R.drawable.notification_downloading);
//mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.downloading_1));
mBuilder.setProgress(100, 0, false);
mBuilder.setAutoCancel(true);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
//Method to update progressbar
public void updateNotification(int progress) {
mBuilder.setProgress(100, progress, false);
mBuilder.setContentText(getString(R.string.downloading) + " " + progress + "%");
mNotificationManager.notify(0, mBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)
notification_downloading.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/downloading_1"
android:duration="@integer/animation_interval_duration" />
<item
android:drawable="@drawable/downloading_2"
android:duration="@integer/animation_interval_duration" />
<item
android:drawable="@drawable/downloading_3"
android:duration="@integer/animation_interval_duration" />
</animation-list>
Run Code Online (Sandbox Code Playgroud)
每次调用"mNotificationManager.notify"都会重新启动动画(仅限Lollipop)
如果我调用"set large icon",则会出现这种情况
我不想设置不确定的进度
这是一个错误吗?谢谢
更新:Android 5.0.1仍在发生
我正在实施通知我的应用程序只是示例
服务类文件:
public class Services
{
public void myNotify(Context context,String message)
{
Log.v("my notification method","from GetNotification class");
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(0,"A new notification", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Log.v("services","11");
//Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(""));
Log.v("services","22");
PendingIntent activity = PendingIntent.getActivity(context,0,new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("")), 0);
Log.v("services","33");
notification.setLatestEventInfo(context,"Notification for you",message,activity);
Log.v("services","44");
notification.number += 1;
Log.v("services","55");
notificationManager.notify(0, notification);
Log.v("services","66");
}
Run Code Online (Sandbox Code Playgroud)
}
AppActivity主类:
public class AndroidPlatformservices extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); …
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(ns);
//Instantiate the notification
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification.Builder builder = new Notification.Builder(c)
.setTicker(tickerText)
.setWhen(when)
.setContentTitle("Test Notification")
.setContentText(arg1.getStringExtra("info"))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
Notification notification = builder.getNotification();
mNotificationManager.notify(88, notification);
Run Code Online (Sandbox Code Playgroud)
它有效,但Notification notification = builder.getNotification();
不推荐使用.正如我应该做的那样Notification notification = builder.build();
.问题是Eclipse没有认识到它,这意味着它不会让我编译.文档很清楚build()
存在并且是首选方法,但它不适用于我的目的.我想使用非弃用的代码,所以任何帮助将不胜感激.
进口
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
Run Code Online (Sandbox Code Playgroud)
请注意,import android.app.Notification.Builder;
它说它没有被使用.
Jelly Bean中引入的扩展通知允许添加操作.这些操作显示为一行中的按钮.如果我想用我自己的自定义视图修改它,我会考虑使用remoteview.
扩展通知是否允许远程视图?我只能在扩展通知存在之前找到在简单通知中使用的远程视图的示例.
android notificationmanager android-notifications android-4.2-jelly-bean android-remoteview
我试图BigTextStyle
在通知中显示多行文本,但无法这样做.我正在使用下面的代码.
public void sendNotification(View view) {
String msgText = "Jeally Bean Notification example!! "
+ "where you will see three different kind of notification. "
+ "you can even put the very long string here.";
NotificationManager notificationManager = getNotificationManager();
PendingIntent pi = getPendingIntent();
android.app.Notification.Builder builder = new Notification.Builder(
this);
builder.setContentTitle("Big text Notofication")
.setContentText("Big text Notification")
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(R.drawable.ic_launcher, "show activity", pi);
Notification notification = new Notification.BigTextStyle(builder)
.bigText(msgText).build();
notificationManager.notify(0, notification);
}
public NotificationManager getNotificationManager() {
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
} …
Run Code Online (Sandbox Code Playgroud) 我最近在让许多特定功能在 Android Studio 上工作时遇到了困难。最近我正在尝试显示简单的通知。他们永远不会出现。在这一点上,我觉得我已经尝试了一切。这是我的代码。
public class NotificationReceiver extends BroadcastReceiver {
private final String TAG = NotificationReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, ScrollingActivity.class);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
taskStackBuilder.addParentStack(ScrollingActivity.class);
taskStackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel");
Notification notification = builder.setContentTitle("My Test Notification")
.setContentText("This is some sample test notification. (Congratulations!)")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}}
Run Code Online (Sandbox Code Playgroud)
这是我用来调用通知的方法。
public void setRepeatingNotification(){
Calendar repeatCalendar = …
Run Code Online (Sandbox Code Playgroud) notifications android notificationmanager notification-channel
我有一个与通知(系统)一起运行的服务。当服务停止(和/或应用程序停止)时,应取消通知(即不再显示在状态栏上)。
通过提供Android Dev guide of Notifications,我没有找到任何关于如何关闭通知的信息。
去SO,我发现了一些问题。
1.
总结@nipun.birla对如何取消 Android 通知的回答:
要取消通知,请按顺序尝试:
NotifcationManager.cancel(int)与notificationID
NotificationManager.cancel(字符串,整数)与notificationID
和notificationTag
但是没有提到的是,如果这些都不起作用,应该怎么做。
2.
一个应该使用cancelAll建议
3.
此 SO 线程包含启动服务示例的一个很好的示例,其中包含在服务生命周期中实现的关联通知(通知也被终止)-有关已启动服务的详细信息,请参见此处
4.
这里提出了删除与通知关联的PendingIntent的建议
5.
还有几个问题和解决方案反映了上述相同的信息:见这个,这个,还有更多……
6.
关于以编程方式在状态栏中隐藏通知图标的一个非常有趣的问题和解决方案
我的问题
现在应该很明显了,我的通知并没有取消自己要求这样做。
执行:
完整的实现见下文,虽然我会发布辅助类和核心函数的一般用法
- 创建通知
private Context context;
private NotificationManager notificationManager;
private NotificationChannel notificationChannel;
private NotificationCompat.Builder notificationBuilder;
public NotificationHelper(Context context) {
this.context = …
Run Code Online (Sandbox Code Playgroud) notifications android android-service notificationmanager android-notifications