使用apk直接安装应用程序时,但上传到Playstore的相同apk无法正常工作

时间:2017-08-31 04:22:08

标签: android apk intentservice

应用程序在使用apk直接安装时正常工作,但上传到Playstore的相同apk无效。

除了在后台运行的意向服务外,所有功能都有效。服务在一段时间后被破坏。

我尝试使用前台服务,但是工作。

MyIntentService.java

public class MyIntentService extends IntentService {

private SQLiteDatabase mDB;
private DataBaseOpenHelper mDbHelper;
private Context mContext;
private String mCropNdviValue, mCropName, mCropId, mCropLat="", mCropLong="", mCropCity="", mCropSubCity="", mCropCreatedAt;
private long mRecordId;
private Session mSession;
private boolean isDataUploaded = false;
String threeSecTime="",latestInsertedValue="";



public MyIntentService() {
    super("MyIntentService");
}

@Override
public void onCreate() {
    super.onCreate();
    mContext = MyIntentService.this;
    mDbHelper = new DataBaseOpenHelper(mContext);
    mDB = mDbHelper.getWritableDatabase();
    mSession = new Session(mContext);

    Log.e("def", "Service is created");
}

@Override
protected void onHandleIntent(Intent intent) {
    while (isDataPresent())
    {
        Log.d("def", "Service starts since data is yet to be uploaded");

        fetchRecordAndUpload();
        Notification notification =
                new Notification.Builder(this)
                        .setContentTitle("Service Status")
                        .setContentText("Service running")
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .build();

        startForeground(102, notification);



    }
    Log.d("def", "Service stops since no data is present");
    stopSelf();
}

private boolean isDataPresent() {


    Log.d("def", "isDataPresent is called");
    String[] projection = {
            DataBaseContract.FeedEntry._ID,
    };



    if (mDB != null) {
        Cursor cursor = mDB.query(
                DataBaseContract.FeedEntry.TABLE_NAME,
                projection,
                null,
                null,
                null,
                null,
                null
        );

        if (cursor.getCount() > 0) {

            return true;

        }
        cursor.close();
    }
    return false;
}

private synchronized void fetchRecordAndUpload() {

    String[] projection = {
            DataBaseContract.FeedEntry._ID,
            DataBaseContract.FeedEntry.COLUMN_NAME_CROP_NDVI_VALUE,
            DataBaseContract.FeedEntry.COLUMN_NAME_CROP_NAME,
            DataBaseContract.FeedEntry.COLUMN_NAME_CROP_ID,
            DataBaseContract.FeedEntry.COLUMN_NAME_CROP_LATITUDE,
            DataBaseContract.FeedEntry.COLUMN_NAME_CROP_LONGITUDE,
            DataBaseContract.FeedEntry.COLUMN_NAME_CROP_CITY,
            DataBaseContract.FeedEntry.COLUMN_NAME_CROP_SUB_LOCALITY,
            DataBaseContract.FeedEntry.COLUMN_NAME_CROP_CREATED_AT
    };

    if (mDB != null) {

        Cursor cursor = null;
        try {
            cursor = mDB.query(
                    DataBaseContract.FeedEntry.TABLE_NAME,
                    projection,
                    null,
                    null,
                    null,
                    null,
                    null
            );


            Log.d("def", "No of records(4) " + cursor.getCount());

            int count = 0;

            while (cursor.moveToNext()) {


                Log.e("def", "While loop starts for " + count);

                mRecordId = cursor.getLong(cursor.getColumnIndexOrThrow(DataBaseContract.FeedEntry._ID));
                mCropNdviValue = cursor.getString(cursor.getColumnIndexOrThrow(DataBaseContract.FeedEntry.COLUMN_NAME_CROP_NDVI_VALUE));
                mCropName = cursor.getString(cursor.getColumnIndexOrThrow(DataBaseContract.FeedEntry.COLUMN_NAME_CROP_NAME));
                mCropId = cursor.getString(cursor.getColumnIndexOrThrow(DataBaseContract.FeedEntry.COLUMN_NAME_CROP_ID));

                mCropLat = cursor.getString(cursor.getColumnIndexOrThrow(DataBaseContract.FeedEntry.COLUMN_NAME_CROP_LATITUDE));
                mCropLong = cursor.getString(cursor.getColumnIndexOrThrow(DataBaseContract.FeedEntry.COLUMN_NAME_CROP_LONGITUDE));
                mCropCity = cursor.getString(cursor.getColumnIndexOrThrow(DataBaseContract.FeedEntry.COLUMN_NAME_CROP_CITY));
                mCropSubCity = cursor.getString(cursor.getColumnIndexOrThrow(DataBaseContract.FeedEntry.COLUMN_NAME_CROP_SUB_LOCALITY));
                mCropCreatedAt = cursor.getString(cursor.getColumnIndexOrThrow(DataBaseContract.FeedEntry.COLUMN_NAME_CROP_CREATED_AT));
                Log.d("def", "Record id fetched from db- " + mRecordId + " Ndvi data- " + mCropNdviValue);

                uploadToServer();
                isDataUploaded = false;
                while (!isDataUploaded) {
                }
                Log.e("def", "While loop ends for " + count);
                count++;
            }

        } finally {
            cursor.close();
        }

    }
}


private void uploadToServer() {


    StringRequest allDealsStringRequest = new StringRequest(Request.Method.POST, Session.baseURl + "ndvi_data_time_insert.php?type=1", new Response.Listener<String>() {

        @Override
        public void onResponse(String response)
        {
            if (response != null && !response.isEmpty())
            {
                try {

                    JSONObject jsonObject = new JSONObject(response);
                    Log.i("response",response);
                    if (jsonObject.getString("Response").equalsIgnoreCase("Success"))
                    {

                        Log.d("def", response);
                        threeSecTime=jsonObject.getString("three_sec_time");
                        mSession.setthreesectime("");
                        if(threeSecTime!=null && threeSecTime!="") {
                            mSession.setthreesectime(threeSecTime);
                        }
                        Log.i("three",threeSecTime);
                        if (deleteRecordFromLocalDB())
                        {
                            Log.d("def", "Record is deleted");
                            isDataUploaded = true;
                        }

                    } else {
                        Log.d("abc", response);

                    }

                } catch (JSONException e) {

                    Log.d("def", "91 " + e.getMessage());
                    e.printStackTrace();


                }

            } else {
                Log.d("def", "99");
                //uploadToServer();
            }


        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            isDataUploaded = true;
            Log.d("def", "56 " + error.getMessage());



        }
    }) {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            Log.d("def", "getParams- " + mSession.getUserId() + " " + mCropNdviValue + " " + mCropCity + " " + mCropLat + " " + mCropLong + " " + mCropSubCity + " " + mCropId + " " + mCropCreatedAt + " " + " " + mSession.getDeviceMacId() + " "+mSession.getthreesectime()+" "+mSession.getpreviousid());
            params.put("user_id", mSession.getUserId());
            params.put("device_id", mSession.getDeviceMacId());
            params.put("ndvi_data", mCropNdviValue.trim());
            if(mCropCity!=null)
            {
                params.put("location", mCropCity.trim());
            }
            else
            {
                params.put("crop_latitude", "");
            }
            if(mCropLat!=null)
            {
                params.put("crop_latitude", mCropLat.trim());
            }else
            {
                params.put("crop_latitude", "");
            }
            if(mCropLong!=null)
            {
                params.put("crop_longitude", mCropLong.trim());
            }else
            {
                params.put("crop_longitude", "");

            }
            if(mCropSubCity!=null)
            {
                params.put("crop_sub_locality", mCropSubCity.trim());
            }else
            {
                params.put("crop_sub_locality", "");

            }
            params.put("crop_id", mCropId.trim());
            params.put("crop_name", mCropName.trim());
            params.put("created_at", mCropCreatedAt.trim());
            params.put("three_update_date",mSession.getthreesectime());





            return params;
        }
    };

    allDealsStringRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    SingletonVolley.getInstance(mContext).addToRequestQueue(allDealsStringRequest);

}

private boolean deleteRecordFromLocalDB() {

    String selection = DataBaseContract.FeedEntry._ID + " LIKE ?";
    String[] selectionArgs = {String.valueOf(mRecordId)};
    return (mDB.delete(DataBaseContract.FeedEntry.TABLE_NAME, selection, selectionArgs) > 0);
}


@Override
public void onDestroy() {
    super.onDestroy();
    Log.e("def", "Service is destroyed");

}
}

2 个答案:

答案 0 :(得分:0)

你好@Dinesh Hebbar,

您需要在Android文档中了解DOZE模式,该文档在Android的所有最新操作系统中实现,在此功能中,您无法访问您的设备或任何网络相关操作他们将自动停止您的后台服务上传或网络相关任务。 https://developer.android.com/training/monitoring-device-state/doze-standby.html

此功能专门用于减少电池使用,

解决方案:您可以使用前台服务。

答案 1 :(得分:0)

当您确定在作业完成后它将被销毁但是如果您想要执行长时间运行的任务,那么您应该使用Intent服务,然后转到后台服务或简单服务。 后台服务: 即使用户关闭了您的应用程序,也不必销毁后台服务,因此您必须自行停止或使用stopself()。 简单的服务: 当用户关闭你的应用程序然后重新启动它时,就会破坏简单的服务。

相关问题