我想通过Android上的后台服务将一些数据上传到Parse.com上的数据库。我之前在活动中使用过Parse并用于在onCreate()方法中编写以下行:
Parse.enableLocalDatastore(this);
Parse.initialize(this, "kw0FN094jFEtdCMrYXNgSYo3wqKhXAEm2RBcLDEq", "AKLRbWFt0ZhWqw1huWA0avuk9iQB7Z1qKVHXSslj");
当我尝试将相同内容添加到我的服务中,然后从我的Android应用程序调用该服务时,logcat会显示以下错误:
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)
我还在AndroidManifest.xml中声明了我的服务。这是一个片段:
<service android:enabled="true" android:name="com.example.shikhar.appname.UploadService"/>
</application>
这是我上传服务的代码:
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() {
}
}
我从主Activity中的单个ImageButton开始并停止服务。这是我的Activity的onCreate()中的代码:
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);
}
}
});
上面的代码使用myActivity类中onCreate之外定义的以下方法:
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;
}
请建议我。我该怎么办?
答案 0 :(得分:1)
您可以尝试在自定义应用程序中初始化解析,因此您不必在每个活动或服务中对其进行初始化:
public class MyAplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(this);
Parse.initialize(this, "XXXX", "XXXX");
}
}
然后在AndroidManifest.xml中添加名称:
<application
android:name=".MyAplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >