Здесь мой код работает до устройств Marshmallow, но в моих устройствах Pie он не работает.
Я прочитал документацию Google и обнаружил удаление тега получателя из манифеста и добавление в активность, но не могу понятьКак добавить.
Основная проблема заключается в том, что при добавлении значения в автономном режиме оно сохраняет значение в SQLite, но при возврате в онлайн-режим не отправляет данные SQLite на сервер выше устройств nogout.
Вот моя деятельность:
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
readFromLocalStorage();
}
};
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SubmitMethode();
// addPremises();
relativeLayoutAdd.setVisibility(View.GONE);
linearLayoutRecycler.setVisibility(View.VISIBLE);
imageButtonPlus.setVisibility(View.VISIBLE);
}
});
}
private void SubmitMethode() {
preName = Objects.requireNonNull(editTextPremises.getText()).toString();
prewifiName = Objects.requireNonNull(editTextwifiName.getText()).toString();
prewifiUrl = Objects.requireNonNull(editTextwifiUrl.getText()).toString();
preserverUrl = Objects.requireNonNull(editTextserverUrl.getText()).toString();
preImage = getStringImage(bitmap);
Log.d("responseT",preName + "==" + prewifiName + "==" + prewifiUrl);
saveToAppServer(preName, prewifiName, prewifiUrl, preserverUrl, preImage);
editTextPremises.setText("");
editTextwifiName.setText("");
editTextwifiUrl.setText("");
editTextserverUrl.setText("");
textViewSelect.setText("Select Image");
}
private void readFromLocalStorage(){
sqPremisesList.clear();
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase sqLiteDatabase = dbHelper.getReadableDatabase();
Cursor cursor = dbHelper.readFromLocalDatabase(sqLiteDatabase);
while (cursor.moveToNext()){
String premisesName = cursor.getString(cursor.getColumnIndex(DBContact.PREMISES_NAME));
String premisesWifiName = cursor.getString(cursor.getColumnIndex(DBContact.PREMISES_WIFI_NAME));
String premisesWifiUrl = cursor.getString(cursor.getColumnIndex(DBContact.PREMISES_WIFIURL));
String premisesServerUrl = cursor.getString(cursor.getColumnIndex(DBContact.PREMISES_SERVERURL));
String premisesImagePath = cursor.getString(cursor.getColumnIndex(DBContact.PREMISES_IMAGEPATH));
int sync_status = cursor.getInt(cursor.getColumnIndex(DBContact.SYNC_STATUS));
sqPremisesList.add(new SqPremises(premisesName, premisesWifiName,
premisesWifiUrl, premisesServerUrl, premisesImagePath, sync_status));
}
//
sqPremisesAdapter.notifyDataSetChanged();
cursor.close();
/*TODO: // this is new line code*/
dbHelper.close();
}
private void saveToAppServer(final String pName, final String pWifiName, final String pWifiUrl,
final String pServerUrl, final String pImage){
if (checkNetworkConnection()){
progressDialog.setTitle("Adding....");
progressDialog.setMessage("please wait while we are waiting your premises");
progressDialog.show();
String Url = Constant.BASE_URL + "addpremises/";
HashMap<String, String> params = new HashMap<String, String>();
params.put("user", userID);
params.put("name", preName);
params.put("wifi_name", prewifiName);
params.put("online_url", prewifiUrl);
params.put("offline_url", preserverUrl);
params.put("image", preImage);
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));
progressDialog.dismiss();
try {
String statusObject = response.getString("status");
String msgObject = response.getString("msg");
if (statusObject.equals("200")) {
Toast.makeText(PremisesManagementActivity.this, msgObject, Toast.LENGTH_SHORT).show();
saveToLocalStorage(pName, pWifiName, pWifiUrl, pServerUrl, pImage, DBContact.SYNC_STATUS_OK);
linearLayoutAsk.setVisibility(View.GONE);
relativeLayoutAdd.setVisibility(View.GONE);
imageButtonPlus.setVisibility(View.VISIBLE);
linearLayoutRecycler.setVisibility(View.VISIBLE);
/* String value = "false";
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("value",value);
editor.commit();*/
}else {
Toast.makeText(PremisesManagementActivity.this, msgObject, Toast.LENGTH_SHORT).show();
// linearLayoutAsk.setVisibility(View.VISIBLE);
// imageButtonPlus.setVisibility(View.VISIBLE);
// linearLayoutRecycler.setVisibility(View.GONE);
// relativeLayoutAdd.setVisibility(View.GONE);
saveToLocalStorage(pName, pWifiName, pWifiUrl, pServerUrl, pImage, DBContact.SYNC_STATUS_FAILED);
}
}catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error.printStackTrace();
saveToLocalStorage(pName, pWifiName, pWifiUrl, pServerUrl, pImage, DBContact.SYNC_STATUS_FAILED);
}
});
requestQueue.add(jsonObjectRequest);
// MySingleton.getInstance(PremisesManagementActivity.this).addToRequestQue(jsonObjectRequest);
}else {
saveToLocalStorage(pName, pWifiName, pWifiUrl,
pServerUrl, pImage, DBContact.SYNC_STATUS_FAILED);
}
}
private void saveToLocalStorage (String pName, String pWifiName, String pWifiUrl,
String pServerUrl,String pImage, int sync){
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
dbHelper.saveToLocalDatabase(pName, pWifiName, pWifiUrl, pServerUrl, pImage, sync , sqLiteDatabase);
readFromLocalStorage();
dbHelper.close();
}
//
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onStart() {
super.onStart();
registerReceiver(broadcastReceiver, new IntentFilter(DBContact.UI_UPDATE_BROADCAST));
// IntentFilter filter = new IntentFilter(DBContact.UI_UPDATE_BROADCAST);
// registerReceiver(broadcastReceiver, filter, Manifest.permission.CHANGE_NETWORK_STATE, null);
// sendBroadcast();
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
И это получатель, который добавляется в файл манифеста:
<receiver android:name=".utils.NetworkMonitor">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
И вот я добавляю свой файл сетевого монитора:
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());
}
Как я могу решить эту проблему?Я новичок в вещании приемника.