我在从通知栏取消正在进行的通知时遇到问题。它可以在 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 中不能使用。 我只是需要从最近的应用程序强制关闭应用程序时取消通知。
请让我知道是否有人对此有解决方案。
以前的版本