I want to upload some data to my database on Parse.com from a background service on Android. I have used Parse in Activities before and used to write the following lines in its onCreate() method:
Parse.enableLocalDatastore(this);
Parse.initialize(this, "kw0FN094jFEtdCMrYXNgSYo3wqKhXAEm2RBcLDEq", "AKLRbWFt0ZhWqw1huWA0avuk9iQB7Z1qKVHXSslj");
When I try to add the same to my service and then call the service from my android app, the logcat shows this error:
07-07 19:43:27.393 1053-1578/com.example.shikhar.trackmydevice E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[UploadService]
Process: com.example.shikhar.trackmydevice, PID: 1053
java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context)`
at com.parse.Parse.enableLocalDatastore(Parse.java:65)
at com.example.shikhar.trackmydevice.UploadService.onHandleIntent(UploadService.java:81)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
I have also declared my service in the AndroidManifest.xml Here's a snippet:
<service android:enabled="true" android:name="com.example.shikhar.appname.UploadService"/>
</application>
This is the code of my Upload Service:
package com.example.shikhar.appname;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class UploadService extends IntentService {
protected static final String TAG = "UploadService: ";
public UploadService() {
super("UploadService");
}
/**
* The IntentService calls this method from the default worker thread with
* the intent that started the service. When this method returns, IntentService
* stops the service, as appropriate.
*/
@Override
protected void onHandleIntent(Intent intent) {
Parse.enableLocalDatastore(this);
Parse.initialize(this, "kw0FN094jFEtdCMrYXNgSYo3wqKhXAEm2RBcLDEq", "AKLRbWFt0ZhWqw1huWA0avuk9iQB7Z1qKVHXSslj");
}
@Override
public void onDestroy() {
}
}
I'm starting and stopping the service from a single ImageButton in main Activity. This is the code in onCreate() of my Activity:
btnService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(thisActivity, UploadService.class);
if(serviceIsRunning(UploadService.class))
{
stopService(intent);
}
else
{
startService(intent);
}
if(serviceIsRunning(UploadService.class))
{
btnService.setImageResource(R.mipmap.ic_service_stop);
}
else
{
btnService.setImageResource(R.mipmap.ic_service_start);
}
}
});
The above code uses the following method defined outside the onCreate inside the class of myActivity:
protected boolean serviceIsRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Please suggest me. What should I do?