2016-10-21 56 views
1

如何在最近的應用程序中刷新後關閉應用程序(或只是刪除所有通知)?在API級別< = 22中,它自動工作(只是強制退出應用程序)但是API級別23 - 棉花糖+應用程序刷卡後未關閉,並且通知也未關閉。Java Android - 在最近的應用程序中刷新後關閉應用程序(通知)

在recnet應用程序中刷卡後如何處理通知或應用程序?

我顯示通知onPause並在onResume上刪除它...但是當我在最近的應用程序中滑動應用程序(應用程序已關閉 - 重新打開應用程序後有新的初始化),但通知仍然在這裏,我需要殺死這個通知。

感謝您的回答傢伙。

BTW:我嘗試服務(onTaskRemoved()),但是這個功能永遠不會被調用,我也不知道爲什麼..

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.util.Log; 

public class MyService extends Service { 

    private static final String TAG = "VYKAL"; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.i(TAG, "onCreate()"); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.i(TAG, "onStartCommand()"); 
     return START_NOT_STICKY; 
    } 

    @Override 
    public void onTaskRemoved(Intent rootIntent) { 
     super.onTaskRemoved(rootIntent); 
     Log.i(TAG, "onTaskRemoved()"); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.i(TAG, "onDestroy()"); 
    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     Log.i(TAG, "onLowMemory()"); 
    } 
} 


super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Intent serviceIntent = new Intent(MainActivity.this, MyService.class); 
     startService(serviceIntent); 

編輯:通知

Intent notificationIntent = new Intent(compat, MainActivity.class); 

     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = PendingIntent.getActivity(compat, 0, 
       notificationIntent, 0); 

     mBuilder = new NotificationCompat.Builder(compat) 
       .setSmallIcon(getSmallIcon(laststate)) 
       .setContentText(lastcontent) 
       .setContentTitle(compat.getString(R.string.APPLICATION_NAME)) 
       .setContentIntent(intent).setOngoing(true); 

     RemoteViews rv = new RemoteViews(compat.getPackageName(), R.layout.notification_layout); 
     rv.setImageViewResource(R.id.imagenotileft, getLargeIconSRC(laststate)); 
     rv.setTextViewText(R.id.title, compat.getString(R.string.APPLICATION_NAME)); 
     rv.setTextViewText(R.id.text, lastcontent); 
     mBuilder.setContent(rv); 

     NotificationManager manager = (NotificationManager) compat.getSystemService(NOTIFICATION_SERVICE); 
     manager.notify(0, mBuilder.build()); 
+0

它是一個棘手的通知? – Viven

+0

IDK什麼是粘滯通知,只是正常通知使用NotificationCompat.Builder @Viv –

+0

您可以在問題的底部看到通知@Viv –

回答

2

解決方案與onTaskRemoved()...如新服務:

public class MyService extends Service { 

    private static final String TAG = "VYKAL"; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.i(TAG, "onCreate()"); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.i(TAG, "onStartCommand()"); 
     return START_NOT_STICKY; 
    } 

    @Override 
    public void onTaskRemoved(Intent rootIntent) { 
     super.onTaskRemoved(rootIntent); 
     MainActivity.closeActivity(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.i(TAG, "onDestroy()"); 
    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     Log.i(TAG, "onLowMemory()"); 
    } 
} 
  • 註冊它在清單
  • 開始它在您的主要活動
相關問題