2013-10-17 18 views
5

通知ボタンをクリックした後でステータスバーを閉じるにはどうすればよいですか?ボタン通知がクリックされたときにステータスバーを閉じる

は私がthisを試してみましたが、私は例外を持っていた:

java.lang.NoSuchMethodException: collapse [] 
    at java.lang.Class.getConstructorOrMethod(Class.java:460) 
    at java.lang.Class.getMethod(Class.java:915) 
    ... 

は私のコード:NotificationIntentクラス

@Override 
public void onReceive(Context context, Intent intent) { 
    int notificationID = intent.getExtras().getInt("NOT_ID"); 
    this.callbackContext = StatusBarNotification.getCallback(); 
    this.mNotificationManager = StatusBarNotification.getNotificationManager(); 

    this.mNotificationManager.cancel(notificationID); 
    this.callbackContext.success(returnJSON(intent)); 
} 

答えて

0

OKのとき、

NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
    .setSmallIcon(R.drawable.icon) 
    .setContentTitle("Sync Failed") 
    .setContentText("Lorem ipsum dolor sit amet") 
    .setStyle(new NotificationCompat.BigTextStyle().bigText("Lorem ipsum dolor sit amet")) 
    .addAction(R.drawable.change, "Change Pass", pChangePass) 
    .addAction(R.drawable.remove, "Ignore", pIgnore) 
    .setAutoCancel(false); 
mNotificationManager.notify(accountUnique, builder.build()); 

、私はそれを解決しました。

private int currentApiVersion = android.os.Build.VERSION.SDK_INT; 
... 

Object sbservice = context.getSystemService("statusbar"); 
try { 
    Class<?> statusbarManager = Class.forName("android.app.StatusBarManager"); 
    if (currentApiVersion <= 16) { 
     Method collapse = statusbarManager.getMethod("collapse"); 
     collapse.invoke(sbservice); 
    } else { 
     Method collapse2 = statusbarManager.getMethod("collapsePanels"); 
     collapse2.invoke(sbservice); 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+4

あなたは本当にサム呂のコメントを受け入れる必要があります - あなたのソリューションは、任意の次のバージョンに取り組んで停止することがあり、それは非使用しているため、公開API。あなたが見ているように、alredyは 'if api <= 16'を追加する必要がありました。すぐに' if api <= 19'を追加する必要があります。 – imbryk

+0

上記のコメントに同意してください。 Samの答えははるかに簡単で、バージョン番号を扱わない。 – Saik

30

次のソリューションは、よりシンプルであるべきで、何の非パブリックAPIを使用しない:

Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
context.sendBroadcast(it); 
+1

これをonRecieveまたはアクションインテントでどこで使用するか教えてください。 – Ajeet

+0

チャームのように仕事!ありがとう。 onRecieveに2行のコード行を挿入するだけです。 –

+0

これはうまくいきました。ありがとう。 – Saik

関連する問題