在后台创建的Andr​​oid地理围栏没有崩溃 [英] create android geofence in background without crashing

查看:256
本文介绍了在后台创建的Andr​​oid地理围栏没有崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有与纬度,经度相关信息接收GCM消息的IntentService,我想注册在后台地理栅栏没有前景的应用程序。我已经试图把(某活动)工作code到IntentService但我得到一个NullPointerException异常,我不知道为什么。

I currently have an IntentService that receives gcm messages with lat,lng infos and I would like to register a geofence in the background without a foreground app. I already tried putting the working (in an activity) code into the IntentService but I get a nullpointerexception and I have no idea why.

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {  
                Double lat = Double.parseDouble(extras.getString("lat"));
                Double lng = Double.parseDouble(extras.getString("lng"));
                Float radius = Float.parseFloat("600");
                createGeofences(lat,lng,radius);

        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

public void createGeofences(Double lat, Double lng, Float radius) {


    mRequestType = GeofenceUtils.REQUEST_TYPE.ADD;
    SimpleGeofence mGeofence = new SimpleGeofence(
        "1",
        lat,
        lng,
        radius,
        GEOFENCE_EXPIRATION_IN_MILLISECONDS,
        Geofence.GEOFENCE_TRANSITION_ENTER);

    // Store this flat version in SharedPreferences
    //mPrefs.setGeofence("1", mGeofence);


    /*
     * Add Geofence objects to a List. toGeofence()
     * creates a Location Services Geofence object from a
     * flat object
     */
    mCurrentGeofences.add(mGeofence.toGeofence());

    try {
        mGeofenceRequester.addGeofences(mCurrentGeofences);
    } catch (UnsupportedOperationException e) {
        Toast.makeText(this, R.string.add_geofences_already_requested_error,
                    Toast.LENGTH_LONG).show();
    }

这是错误消息:

java.lang.NullPointerException
at com.app.portalalert.GcmIntentService.createGeofences(GcmIntentService.java:142)
at com.app.portalalert.GcmIntentService.onHandleIntent(GcmIntentService.java:85)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)

任何意见或提示我怎么能做到这一点?

Any ideas or tips how I could achieve this?

推荐答案

初始化mCurrentGeofences是这样的:

Initialize mCurrentGeofences like this:

List<Geofence> mCurrentGeofences = new ArrayList<Geofence>(); 

而不是只:

List<Geofence> mCurrentGeofences; 

这篇关于在后台创建的Andr​​oid地理围栏没有崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆