2017-08-31 16 views
0

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"); 

} 
} 
+0

サービスが破棄されたときにリスナーを追加してから、リスナーを再起動して確認してください。 –

+2

インテントサービスの代わりにサービスを使用する方が良いでしょう。インテントサービスは一度処理を終えた後に自分自身を破棄します。 – Reena

+0

フローをトレースするためにいくつかのコメントを追加しました。サービスは実行中ですがデータはアップロードされません私はアプリからアプリをクリアして再起動すると、アプリを開いた直後にすべてのデータがアップロードされます。 –

答えて

0

こんにちは@Dinesh Hebbar、あなたは、Androidのすべての最新のOSに実装Androidの文書でDOZEモードについて学ぶ必要が

、この機能では、あなたはそうではありませんあなたのデバイスやネットワーク関連の操作にアクセスする彼らはあなたのバックグラウンドサービスのアップロードやネットワーク関連の仕事を自動的に停止します。 https://developer.android.com/training/monitoring-device-state/doze-standby.html

特別にバッテリーが使用削減のための実装この機能、

ソリューション:あなたはフォアグラウンドサービスを使用することができます。

+0

返信いただきありがとうございます、私はこの解決策でそれをチェックします。 –

0

インテントサービスは、ジョブの実行後に破棄されますが、長時間実行されるタスクを実行する場合は、バックグラウンドサービスまたはシンプルサービスを実行することを確認する必要があります。 バックグラウンドサービス: ユーザーがアプリケーションを終了したため、バックグラウンドサービスが破壊されることはありません。自分で停止するか、stopself()を使用する必要があります。 シンプルサービス: シンプルなサービスは、ユーザーがアプリケーションを終了してから再起動すると破棄されます。

+0

はい、それは私の悪い私はフォアグランドサービスを意味した。 –

関連する問題