2013-10-17 19 views
5

Wie kann ich die Statusleiste nach dem Klicken auf die Benachrichtigungsschaltfläche schließen?Schließen Sie die Statusleiste, wenn Sie auf die Schaltfläche klicken.

ich this versucht, aber ich hatte eine Ausnahme:

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

Mein Code:

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()); 

Bei NotificationIntent Klasse

@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)); 
} 

Antwort

0

Ok, ich löste es.

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

Sie sollten wirklich Sam Lu Kommentar akzeptieren - Ihre Lösungen auf irgendwelchen nächsten Versionen aufhören zu arbeiten könnten, weil sie nicht verwendet öffentliche api. Wie Sie sehen, mussten Sie bereits 'if api <= 16 'hinzufügen - es ist möglich, dass Sie bald' if api <= 19' hinzufügen müssen. – imbryk

+0

Mit dem obigen Kommentar einverstanden. Sams Antwort ist viel einfacher und behandelt keine Versionsnummern. – Saik

30

Die folgende Lösung sollte einfacher und keine nicht-öffentlichen APIs verwendet werden:

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

Können Sie sagen, wo Sie dies verwenden sollen, in onRecieve oder Action Intent? – Ajeet

+0

Arbeit wie Charme! Vielen Dank. Setzen Sie einfach 2 Codezeilen in onRecieve. –

+0

Ja, das hat funktioniert. Vielen Dank. – Saik

Verwandte Themen