0
I have an android application where after the user enters his company ID he activates a switch and from there the application is posting in an API the position of it. This is done through the workmanager.
Follow my service class
public class TestWorker extends Worker {
public TestWorker(
@NonNull Context context,
@NonNull WorkerParameters params) {
super(context, params);
}
@Override
public Result doWork() {
// Log.d("DEBUG: ", "Serviço worker startando");
String date = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault()).format(new Date());
String ra = getInputData().getString("ra");
// Log.d("DEBUG", "ra que chega: "+ra);
try {
testPost(ra, date);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Indicate whether the task finished successfully with the Result
return Result.success();
}
private void sendData(final String user_id, final String busca, final String data) {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url = "http://200.134.18.125:5000/api/v1/resources/positions/app";
JSONObject js = new JSONObject();
try {
js.put("user_id", user_id);
js.put("search", busca);
js.put("date", data);
Log.d("DEBUG json: ", "" + js);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, js,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Log.d("DEBUG: ", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Log.d("Error: ", "" + error.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
queue.add(jsonObjectRequest);
}
private void testPost(String ra, String datetime) throws ExecutionException, InterruptedException {
//String datetime = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault()).format(new Date());
//Log.d("DEBUG", "testando data: "+datetime);
//String ra = "1111112";
String search = "-100,-72,-74,-59,-79,-100"; //B8A
// Log.d("DEBUG", "Busca: "+search+", ra: "+ra+", Data: "+datetime);
sendData(ra,search, datetime);
}
}
And here as I start in Mainactivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Switch aSwitch = findViewById(R.id.switch1);
final EditText editText = findViewById(R.id.ra);
Constraints constraints = new Constraints.Builder()
.setRequiresCharging(true)
.build();
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.d("DEBUG", "Começou..");
editText.setEnabled(false);
Data.Builder data = new Data.Builder();
data.putString("ra", editText.getText().toString());
PeriodicWorkRequest testWork = new PeriodicWorkRequest.Builder(TestWorker.class, 15, TimeUnit.MINUTES)
.addTag("APIPosting")
.setInputData(data.build())
.setConstraints(constraints)
.build();
WorkManager.getInstance(getApplicationContext())
.enqueue(testWork);
switchOnMsg();
Log.d("DEBUG", "Service Status: "+ (WorkManager.getInstance().getWorkInfoById(testWork.getId())));
//WorkManager.getInstance().cancelAllWorkByTag("APIPosting");
//Log.d("DEBUG", "Service Status apos cancelar: "+ (WorkManager.getInstance().getWorkInfoById(testWork.getId())));
}else{
switchOffMsg();
editText.setEnabled(true);
//Log.d("DEBUG","Testwork: "+testWork);
WorkManager.getInstance().cancelAllWorkByTag("APIPosting");
//WorkManager.getInstance().cancelAllWork();
//Log.d("DEBUG", "Service Status: "+ (WorkManager.getInstance().getWorkInfoById(testWork.getId())));
}
}
});
To the problem:
This all works the way I want it. It puts the position in the API every 15 min normally. But if I do it without the USB cable connected with the IDE it stops working. I noticed that the status of the service is like PENDING when it is without the cable, after connecting the cable again in the PC it goes back to do the posts in the API.
Am I missing something obvious?
My manifest is that way:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BootCompletedIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".ScanService"></service>
<service android:name=".TestService"/>
</application>
What I need is to know how to get my app to do the posts even without being connected to the USB cable in the IDE.
Additional details: As it is a location app where the user activates or not the location of the same he must run in the background from the moment the switch is activated even if the phone blocks the screen