如何在通知发送PendingIntent到服务后隐藏通知面板 [英] How to hide Notification Panel after sending a PendingIntent to Service in a notification

查看:203
本文介绍了如何在通知发送PendingIntent到服务后隐藏通知面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有

我写了一个服务来更新系统状态,我用startForeground把服务前台,还加入了通知吧。在通知中,我使用远程视窗有三个图像有三个OnClickPendingIntent。其中一人被发回给服务,并执行通知更新codeS。

code创建通知:

 意图int​​entApp =新的意图(这一点,ScreenOffWidgetConfigure.class);
 intentApp.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
 PendingIntent piApp = PendingIntent.getActivity(此,0,intentApp,0);

 意图int​​entOnOff =新的意图(CV.SERVICE_INTENT_ACTION);
 intentOnOff.putExtra(CV.SERVICEACTION,CV.SERVICEACTION_TOGGLE);
 PendingIntent piOnOff = PendingIntent.getService(此,0,intentOnOff,0);

 意图int​​entScreenOff =新的意图(CV.SERVICE_INTENT_ACTION);
 intentScreenOff.putExtra(CV.SERVICEACTION,CV.SERVICEACTION_SCREENOFF);
 PendingIntent piScreenOff = PendingIntent.getService(此,1,intentScreenOff,0);

 //设置远程视窗
 RemoteViews remoteViews =新RemoteViews(getPackageName(),R.layout.layout_notification);
 remoteViews.setOnClickPendingIntent(R.id.image_logo,piApp);
 remoteViews.setOnClickPendingIntent(R.id.image_status,piOnOff);
 remoteViews.setOnClickPendingIntent(R.id.image_screenoff,piScreenOff);

 如果(CV.get prefChargingOn(本)及和放大器; CV.isPlugged(本))
 {
     remoteViews.setImageViewResource(R.id.image_status,R.drawable.widget_charging_on);
 }
 其他
 {
     如果(CV.get prefAutoOnoff(本))
     remoteViews.setImageViewResource(R.id.image_status,R.drawable.widget_on);
     其他
     remoteViews.setImageViewResource(R.id.image_status,R.drawable.widget_off);
 }

 //建立通知
 通知NotI位=新Notification.Builder(本)
 .setContent(remoteViews)
 .setSmallIcon(R.drawable.ic_launcher)
 .setOngoing(真)
 。建立();

 回报NotI位;
 

code更新notificaton,接到意向后:

 通知通知= createNotification();
 最后NotificationManager notificationManager =(NotificationManager)getApplicationContext()
                        .getSystemService(getApplicationContext()NOTIFICATION_SERVICE。);

 notificationManager.notify(NOTIFICATION_ONGOING通知);
 

我的问题是:叫我的更新功能后,通知图像实际上是改变;然而,通知面板依然存在!! 它不会消失,就像它应该是什么样的发射活动。

有一个标志,我可以通知,pendingIntent设置,或者是API调用接收意向的售后服务,我可以使用?

解决方案

找到我的问题回答了在其他地方:
1。<一个href="http://stackoverflow.com/questions/13766789/android-how-to-collapse-status-bar-on-android-4-2">Android:如何崩溃在Android 4.2?状态栏 2. preventing状态栏扩展

首先,添加使用许可行的Andr​​oidManifest.xml

 &LT;使用-权限的Andr​​oid:名称=android.permission.EXPAND_STATUS_BAR/&GT;
 

第二,使用下面的codeS,使其工作:

 尝试
{
    对象服务= getSystemService(状态栏);
    类&LT;&GT; statusbarManager =的Class.forName(android.app.StatusBarManager);
    如果(Build.VERSION.SDK_INT&LT; = 16)
    {
        方法塌陷= statusbarManager.getMethod(崩溃);
        collapse.setAccessible(真正的);
        collapse.invoke(服务);
    }
    其他
    {
        方法collapse2 = statusbarManager.getMethod(collapsePanels);
        collapse2.setAccessible(真正的);
        collapse2.invoke(服务);
    }
}赶上(例外前){}
 

这是哈克,但它确实工作。

All

I write a service to to update system state, and I use startForeground to put the service to foreground, also adding a notification to it. In the notification, I use remoteView to have three images with three OnClickPendingIntent. One of them is sending back to the service, and do the notification update codes.

code for creating notification:

 Intent intentApp = new Intent(this,ScreenOffWidgetConfigure.class);
 intentApp.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
 PendingIntent piApp = PendingIntent.getActivity(this, 0, intentApp, 0);

 Intent intentOnOff = new Intent(CV.SERVICE_INTENT_ACTION);
 intentOnOff.putExtra(CV.SERVICEACTION, CV.SERVICEACTION_TOGGLE);
 PendingIntent piOnOff = PendingIntent.getService(this, 0, intentOnOff, 0);

 Intent intentScreenOff = new Intent(CV.SERVICE_INTENT_ACTION);
 intentScreenOff.putExtra(CV.SERVICEACTION, CV.SERVICEACTION_SCREENOFF);
 PendingIntent piScreenOff = PendingIntent.getService(this, 1, intentScreenOff, 0);

 // setup remoteview
 RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);
 remoteViews.setOnClickPendingIntent(R.id.image_logo, piApp);
 remoteViews.setOnClickPendingIntent(R.id.image_status, piOnOff);
 remoteViews.setOnClickPendingIntent(R.id.image_screenoff, piScreenOff);

 if(CV.getPrefChargingOn(this) && CV.isPlugged(this))
 {
     remoteViews.setImageViewResource(R.id.image_status,R.drawable.widget_charging_on);
 }
 else
 {
     if(CV.getPrefAutoOnoff(this))
     remoteViews.setImageViewResource(R.id.image_status,R.drawable.widget_on);
     else
     remoteViews.setImageViewResource(R.id.image_status,R.drawable.widget_off);
 }

 // build the notification
 Notification noti = new Notification.Builder(this)
 .setContent(remoteViews)
 .setSmallIcon(R.drawable.ic_launcher)
 .setOngoing(true)
 .build();

 return noti;

code for updating notificaton, after receiving the intent:

 Notification notify = createNotification();
 final NotificationManager notificationManager = (NotificationManager) getApplicationContext()
                        .getSystemService(getApplicationContext().NOTIFICATION_SERVICE);

 notificationManager.notify(NOTIFICATION_ONGOING, notify);

My question is: after calling my updating function, the notification image is actually changed; however, the notification panel is still there!! It does not disappear just like what it should be for launching activities.

Is there a flag I can set for notification, pendingIntent, or what API calls I can use after receiving the intent in service?

解决方案

find my question answered in other place:
1. Android: How to collapse status bar on android 4.2? 2. Preventing status bar expansion

first, add use permission lines to AndroidManifest.xml

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>

second, use following codes to make it work:

try
{
    Object service  = getSystemService("statusbar");
    Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
    if (Build.VERSION.SDK_INT <= 16) 
    {
        Method collapse = statusbarManager.getMethod("collapse");
        collapse.setAccessible(true);
        collapse.invoke(service);
    } 
    else 
    {
        Method collapse2 = statusbarManager.getMethod("collapsePanels");
        collapse2.setAccessible(true);
        collapse2.invoke(service);
    }
}catch(Exception ex){}

it's hacky, but it did work.

这篇关于如何在通知发送PendingIntent到服务后隐藏通知面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆