2

全て

システム状態を更新するサービスを作成し、startForeground を使用してサービスをフォアグラウンドにし、通知も追加します。通知では、remoteView を使用して、3 つの OnClickPendingIntent を持つ 3 つの画像を表示しています。それらの 1 つは、サービスに送り返すことであり、更新コードの通知を行います。

通知を作成するためのコード:

 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 呼び出しに設定できるフラグはありますか?

4

1 に答える 1

4

他の場所で私の質問の回答を見つけてください:
1. Android: Android 4.2 でステータス バーを折りたたむ方法は? 2.ステータスバーの拡大防止

まず、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){}

ハックですが、うまくいきました。

于 2013-06-07T07:16:11.380 に答える