【问题标题】:Using Parse.com in Android Intent Service在 Android Intent 服务中使用 Parse.com
【发布时间】:2015-07-07 14:45:36
【问题描述】:

我想从 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 中声明了我的服务这是一个 sn-p:

    <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;
}

请给我建议。我该怎么办?

【问题讨论】:

    标签: android parse-platform intentservice android-intentservice


    【解决方案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" >
    

    【讨论】:

    猜你喜欢
    • 2018-02-09
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多