当应用程序在Oreo中被杀死时,如何取消正在进行的通知?

时间:2018-12-29 07:12:37

标签: android notifications android-service android-8.0-oreo notificationmanager

我在从通知栏取消正在进行的通知时遇到问题。它可以在 Service 中使用if (checkNetworkConnection(context)){ final DBHelper dbHelper = new DBHelper(context); final SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase(); Cursor cursor = dbHelper.readFromLocalDatabase(sqLiteDatabase); while (cursor.moveToNext()){ int sync_status = cursor.getInt(cursor.getColumnIndex(DBContact.SYNC_STATUS)); if (sync_status == DBContact.SYNC_STATUS_FAILED){ final String premisName = cursor.getString(cursor.getColumnIndex(DBContact.PREMISES_NAME)); final String premisWifiName = cursor.getString(cursor.getColumnIndex(DBContact.PREMISES_WIFI_NAME)); final String premisWifiUrl = cursor.getString(cursor.getColumnIndex(DBContact.PREMISES_WIFIURL)); final String premisServerUrl = cursor.getString(cursor.getColumnIndex(DBContact.PREMISES_SERVERURL)); final String premisesImagePath = cursor.getString(cursor.getColumnIndex(DBContact.PREMISES_IMAGEPATH)); String Url = Constant.BASE_URL + "addpremises/"; HashMap<String, String> params = new HashMap<String, String>(); params.put("user", userID ); params.put("name", premisName); params.put("wifi_name", premisWifiName); params.put("online_url", premisWifiUrl); params.put("offline_url", premisServerUrl); params.put("image",premisesImagePath); Log.d("responseV", String.valueOf(params)); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Url, new JSONObject(params), new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d("responseJ", String.valueOf(response)); try { String statusObject = response.getString("status"); String msgObject = response.getString("msg"); if (statusObject.equals("200")) { dbHelper.updateLocalDatabase(premisName, premisWifiName, premisWifiUrl, premisServerUrl, premisesImagePath, DBContact.SYNC_STATUS_OK, sqLiteDatabase); context.sendBroadcast(new Intent(DBContact.UI_UPDATE_BROADCAST)); } }catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("responseE", String.valueOf(error.getMessage())); } }); requestQueue.add(jsonObjectRequest); } } } } public boolean checkNetworkConnection(Context context){ ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } 在android的早期版本上完美运行,但在 Oreo 中不能使用。 我只是需要从最近的应用程序强制关闭应用程序时取消通知。 请让我知道是否有人对此有解决方案。 以前的版本

1 个答案:

答案 0 :(得分:1)

更新

如果您强制关闭您的应用程序,则不会调用onDestroy,并且您没有选择权来拦截此操作。

HereHere是关于它的两个参考,您可以找到更多

....................

将这些行添加到您的服务中

@Override
public void onDestroy() {
    super.onDestroy();
    stopForeground(true);

}