我想从通知栏中显示的其他应用程序中删除通知.那可能吗?NotificationManager.cancelAll();据我所知,仅取消调用应用程序显示的通知.
我为什么要这样做?
我有一个通过网页读取和发送短信的应用程序,我希望这个应用程序与现有的SMS应用程序如Handcent SMS共同退出.我希望它的工作方式是,当通过网页阅读新收到的短信时,我想取消Handcent SMS显示的新SMS通知,因为现在已经读取了新短信.我正在将短信标记为已读,并且一旦我打开Handcent SMS,Handcent SMS就会将其识别为已读取,但是直到我点击实际的短信,通知仍然存在.
有两种情形; 1号:
Senario 2:
这可能吗?
Jelly Bean添加了对可扩展状态通知的支持.根据http://developer.android.com/about/versions/jelly-bean.html:
除了模板化样式,您还可以使用任何远程视图创建自己的通知样式.
怎么办?我相信你需要创建一个自定义的Notification.Style.这是一个抽象类,所以我需要扩展它.我无法找到有关哪些部分需要扩展的文档.
这个SO问题给出了如何使用notificaiton.builder进行基本通知的一个很好的例子,我将此作为起点.添加
.setContent(new RemoteViews(getPackageName(), R.layout.notification))
Run Code Online (Sandbox Code Playgroud)
为基本通知添加自定义视图,但它不可扩展.
我从服务器下载图像作为位图并将其转换为drawable现在我想使用这个drawable作为通知图标.但我无法做到这一点.这是我的代码:
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(intent)
.setSmallIcon(bitmap)
.setWhen(when)
.build();
Run Code Online (Sandbox Code Playgroud)
但是icon是一个Resources int值,所以当我使用它时会出错.任何帮助
编辑:
现在我更新我的代码,现在我这样做:
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(intent)
.setSmallIcon(icon)
.setLargeIcon(bitmap)
.setWhen(when)
.build();
Run Code Online (Sandbox Code Playgroud)
但它左侧有大图标,右侧有小图标.我不希望这样,为此我删除setSmallIcon行并运行我的代码,但它没有显示通知
我尝试创建自定义通知但获得以下异常:
FATAL EXCEPTION: main
android.app.RemoteServiceException: Bad notification posted from package com.my.app: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.my.app user=UserHandle{0} id=1 tag=null score=0: Notification(pri=0 contentView=com.my.app/0x7f03001b vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null]))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1423)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样:
Intent intent = new Intent(this, MyFragmentActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent launchIntent = PendingIntent.getActivity(this, 0, intent, 0);
RemoteViews notificationView = new RemoteViews(this.getPackageName(), R.layout.notification_layout);
notificationView.setImageViewBitmap(R.id.notification_icon, icon);
notificationView.setTextViewText(R.id.present_text, "Text Test");
Notification.Builder builder = new Notification.Builder(getApplicationContext()); …Run Code Online (Sandbox Code Playgroud) 当我从通知中启动它时,我的某个应用程序出现问题.它从未出现在"最近的应用"列表中.
没有通知,每件事都按预期工作:我启动应用程序,导航进入,当我退出它时(使用主页按钮或后退按钮)之后,我可以通过长按主页按钮=>确定返回到它.
当我收到通知时,问题就开始了.如果我从通知启动应用程序,它会启动正确的屏幕,我可以使用app => ok.但是当我退出应用程序(使用主页或后退按钮)时,它不再出现在"最近的应用程序"列表中.更确切地说:
下面是我在状态栏中添加通知的原始代码:
mNotification = new Notification(R.drawable.ic_notif, message, System.currentTimeMillis());
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(link));
notificationIntent.putExtra(...);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
mNotification.setLatestEventInfo(context, context.getString(R.string.app_name), message, contentIntent);
notificationManager.notify(ConfigApp.NOTIFICATION_ID, mNotification);
Run Code Online (Sandbox Code Playgroud)
我尝试添加FLAG_ACTIVITY_NEW_TASK但它没有帮助:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(link));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra(...);
Run Code Online (Sandbox Code Playgroud)
通知启动的活动的清单声明:
<activity
android:name=".activity.ActivityMessages"
android:label=""
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="hostapp"
android:pathPrefix="/prefixapp"
android:scheme="schemeapp" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
有人知道如何在通知启动后将应用程序保留在"最近的应用程序"列表中吗?
我想在我的应用程序中添加一个Notification操作项,这是一个音乐播放器.启动流时,应触发通知,并在通知中显示流的停止按钮.到目前为止通知工作正常,我遇到了停止操作项目的问题.以下是在启动流的服务中声明它的方式:
Intent stopIntent = new Intent(this, MusicPlayerNew.class);
stopIntent.putExtra("STOP", "STOP");
PendingIntent stopPendingIntent = PendingIntent.getActivity(this, 0,
stopIntent, PendingIntent.FLAG_UPDATE_CURRENT, null);
mBuilder.addAction(R.drawable.ic_stat_stop, "Stop", stopPendingIntent);
Run Code Online (Sandbox Code Playgroud)
现在在onResume() - 我的活动方法中,我用getIntent().getStringExtra()检查"STOP"额外,但我通过getIntent()检索的意图没有设置额外的东西:(
我还试图检查发送广播(我有一个广播接收器工作从服务到活动进行通信)
Intent stopIntent2 = new Intent(MusicPlayerNew.STOP_MEDIAPLAYER);
PendingIntent stopPendingIntent2 = PendingIntent.getBroadcast(this, 0,
stopIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.ic_stat_stop, "Stop", stopPendingIntent2);
Run Code Online (Sandbox Code Playgroud)
现在,如果活动当前位于前台,则此功能正常.如果活动在后台,则停止按钮不执行任何操作:(
编辑: 我在我的Activity中将BroadcastReceiver作为私有类
private class DataUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
..
}}
Run Code Online (Sandbox Code Playgroud)
在onResume()中注册我的app接收器:
intentFilter = new IntentFilter(STOP_MEDIAPLAYER);
registerReceiver(dataUpdateReceiver, intentFilter);
Run Code Online (Sandbox Code Playgroud)
的onPause()
unregisterReceiver(dataUpdateReceiver);
Run Code Online (Sandbox Code Playgroud)
现在,如果我从onPause() - 方法中删除取消注册,即使app/activity不在前台,也会收到广播.但这是正确的方法吗?我从网上的教程中得到了这个注册/取消注册的内容我认为..
android android-intent android-notifications android-activity android-pendingintent
我有一个Media服务,它使用startForeground()在播放开始时显示通知.播放时有暂停/停止按钮,暂停时有播放/停止按钮.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
// setup...
Notification n = mBuilder.build();
if (state == State.Playing) {
startForeground(mId, n);
}
else {
stopForeground(false);
mNotificationManager.notify(mId, n);
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是当我显示/更新处于暂停状态的通知时,应该允许您将其删除.mBuilder.setOngoing(false)似乎没有效果,因为之前startForeground覆盖它.
stopForeground(true);使用相同的代码调用按预期工作,但通知在销毁和重新创建时闪烁.有没有办法"更新"从startForeground创建的通知,以允许在调用stop后删除它?
编辑:根据要求,这是创建通知的完整代码.只要播放或暂停服务,就会调用createNotification.
private void createNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("No Agenda")
.setContentText("Live stream");
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
if (state == State.Playing) {
Intent pauseIntent = new Intent(this, MusicService.class);
pauseIntent.setAction(ACTION_PAUSE);
PendingIntent pausePendingIntent = PendingIntent.getService(MusicService.this, 0, pauseIntent, 0);
mBuilder.addAction(R.drawable.pause, "Pause", pausePendingIntent);
//mBuilder.setOngoing(true);
}
else if …Run Code Online (Sandbox Code Playgroud) android android-service android-notifications android-notification-bar
我知道你可以在推送通知参数中发送信息,如消息,标题,图片URL等.Facebook如何在通知区域显示您的个人资料照片?我想在通知区域中使用外部图像,因此当您将其向下拉时,您会看到包含该消息的个人资料图像.现在,我的只显示drawable文件夹中的默认图标.我认为这可能是一个常见的问题,但找不到任何东西.你能帮忙的话,我会很高兴.
android push-notification android-layout android-notifications
我正在尝试实现在Android 4.3中添加的NotificationListnerService,但我无法获取通知详细信息.
我的代码如下
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("notification test");
mBuilder.setContentText("Notification text");
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个使用的演示setVisibility()来控制Android 5.0锁屏上显示的内容Notification.但是,似乎没有效果:
默认设置VISIBILITY_PRIVATE仍然显示私有Notification,而不是公共对应
VISIBILITY_SECRET 通知仍会显示在锁定屏幕上
IOW,一切都表现得好像VISIBILITY_PUBLIC有效,至少当我测试运行Android 5.0图像的Nexus 7时,我们在Android 5.0发布后不久就给出了(构建LPX13D).所以我不知道问题是否与我的代码,此设备或Android中的错误有关.
我有两个版本的相同示例应用程序:
一个使用NotificationCompat和NotificationManagerCompat
其他的用途Notification和NotificationManager使用minSdkVersion的21和targetSdkVersion21
(请注意,这些项目主要用于Android Studio; Eclipse用户可以导入项目,但可能需要进行少量修复,特别是对于support-v13第一个示例的库引用)
样本用于AlarmManager触发Notification工作,主要是因为您有机会返回锁定屏幕以查看结果.这是BroadcastReceiver由AlarmManager(显示NotificationCompat版本)触发的:
/***
Copyright (c) 2014 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may …Run Code Online (Sandbox Code Playgroud)