【问题标题】:How to hide Notification Panel after sending a PendingIntent to Service in a notification如何在通知中向服务发送 PendingIntent 后隐藏通知面板
【发布时间】:2013-06-06 15:47:15
【问题描述】:

全部

我编写了一个服务来更新系统状态,并使用 startForeground 将服务置于前台,同时向其添加通知。在通知中,我使用 remoteView 来获得三个带有三个 OnClickPendingIntent 的图像。其中之一是发送回服务,并执行通知更新代码。

创建通知的代码:

 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;

收到意图后更新通知的代码:

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

 notificationManager.notify(NOTIFICATION_ONGOING, notify);

我的问题是:调用我的更新函数后,通知图像实际上发生了变化;但是,通知面板还在!!它并没有像启动活动那样消失。

是否可以为通知、pendingIntent 或在服务中接收到意图后我可以使用哪些 API 调用设置标志?

【问题讨论】:

    标签: android service notifications


    【解决方案1】:

    在其他地方找到我的问题:
    1.Android: How to collapse status bar on android 4.2? 2.Preventing status bar expansion

    首先,在 AndroidManifest.xml 中添加使用权限行

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

    其次,使用以下代码使其工作:

    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){}
    

    这很hacky,但确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多