在 Android 意图服务中使用 Parse.com
Using Parse.com in Android Intent Service
我想从 Android 上的后台服务将一些数据上传到我在 Parse.com 上的数据库。我之前在 Activity 中使用过 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);
}
}
});
上面的代码使用了下面的定义在onCreate之外的方法在myActivity的class里面:
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;
}
请推荐我。我该怎么办?
您可以尝试在您的自定义应用程序中初始化解析,这样您就不必在每个 activity 或服务中初始化它:
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" >
我想从 Android 上的后台服务将一些数据上传到我在 Parse.com 上的数据库。我之前在 Activity 中使用过 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);
}
}
});
上面的代码使用了下面的定义在onCreate之外的方法在myActivity的class里面:
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;
}
请推荐我。我该怎么办?
您可以尝试在您的自定义应用程序中初始化解析,这样您就不必在每个 activity 或服务中初始化它:
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" >