apkを使用して直接インストールするとアプリケーションが動作しますが、プレイストアにアップロードされたapkは機能しません。apkを使用して直接インストールするとアプリケーションが動作しますが、プレイストアにアップロードされた同じapkは動作しません。
バックグラウンドで実行されているインテントサービスを除いて、すべての機能が動作しています。サービスはある時間間隔の後に破壊されています。
私はフォアグラウンドサービスを使用しようとしましたが、dint作業を使用しました。
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");
}
}
サービスが破棄されたときにリスナーを追加してから、リスナーを再起動して確認してください。 –
インテントサービスの代わりにサービスを使用する方が良いでしょう。インテントサービスは一度処理を終えた後に自分自身を破棄します。 – Reena
フローをトレースするためにいくつかのコメントを追加しました。サービスは実行中ですがデータはアップロードされません私はアプリからアプリをクリアして再起動すると、アプリを開いた直後にすべてのデータがアップロードされます。 –