GeofenceClient.addGeofences()无法启动IntentService

时间:2018-07-17 15:14:03

标签: android android-studio

我正在尝试使用Google的地理围栏,并且遵循Google文档:enter link description here 问题是,当我调用addGeofences()时,它应该启动IntentService,但它只是停止了。 addGeofences()结果始终显示成功,并且我的Log enter image description here则什么也没有发生, 我的IntentService的onHandleIntent()日志未显示任何内容,我认为它甚至没有被激活。 我使用Nexus5 API25模拟器和Target Android7.1.1(Google API)进行测试。 我进行了很多搜索,但仍然无法理解为什么,也许我错过了一些东西。请帮我,对不起我的英语不好。

这是我的代码:

 private void startGeofenceMonitor()
{
    Log.i("info", "Start geofence monitoring");
    try
    {
        int permission = ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION);
        if (permission != PackageManager.PERMISSION_GRANTED)
        {              
            ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION} , PERMISSIONS_REQUEST_CODE);
            Log.i("info", "Permission not available! ");
        }
        else
        {
            mGeofencingClient = LocationServices.getGeofencingClient(this);
            geofenceList.add(new Geofence.Builder().setRequestId("point0")
                    .setCircularRegion(55, 55, 15)
                    .setExpirationDuration(Geofence.NEVER_EXPIRE)
                    .setNotificationResponsiveness(1000)
                    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                    .build());
            mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
                    .addOnSuccessListener(this, new OnSuccessListener<Void>()
                    {
                        @Override
                        public void onSuccess(Void aVoid)
                        {
                            Log.i("info", "geofence added!: ");
                        }
                    })
                    .addOnFailureListener(new OnFailureListener()
                    {
                        @Override
                        public void onFailure(@NonNull Exception e)
                        {
                            Log.i("info", "geofence failed! " + e.toString());
                        }
                    });
        }
    }
    catch (Exception e)
    {
        Log.i("er", e.getMessage());
    }
}
 private GeofencingRequest getGeofencingRequest()
{
    return new GeofencingRequest.Builder()
            .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL)
            .addGeofences(geofenceList)
            .build();
}
private PendingIntent getGeofencePendingIntent()
{
    if (mGeofencePendingIntent != null)
    {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    mGeofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    return mGeofencePendingIntent;
}

和我的IntentService

 public GeofenceTransitionsIntentService()
{
    super("GeofenceTransitionsIntentService");
    Log.i("info", "IntentService is activated!");
}
@Override
protected void onHandleIntent(Intent intent)
{
    String test = intent.getStringExtra("pls");
    Log.i("info", "onHandleIntent is activated!");
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    Log.i("info", "geofencingEvent size: " + intent.toString());
    if (geofencingEvent.hasError())
    {
        Log.e("er", "geofencingEvent has error!");
        return;
    }
    else
    {
    }
    int geofenceTransition = geofencingEvent.getGeofenceTransition();
    Log.e("er", "geofenceTransition" + geofenceTransition);
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER)
    {
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
        Geofence geofence = triggeringGeofences.get(0);
        String requestID = geofence.getRequestId();
        Log.i("geo", "Entering geofence : " + requestID);
    }
    else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT)
    {
        Log.i("geo", "Exiting geofence : ");
    }
    else
    {
        Log.i("geo", "geo not in range ");
    }
}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hs.geofencestest">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".GeofenceTransitionsIntentService" android:exported="true"/>
</application>

0 个答案:

没有答案