2

觸發地理圍欄過渡IntentService我想,即使應用程序是不是在後臺動態更新根據用戶當前位置的地理圍欄的列表。所以我從服務而不是活動呼叫GeofencingApi.addGeofences無法從的PendingIntent

public void addGeofences() 
{ 

    if (!mGoogleApiClient.isConnected()) { 
     Log.v("TAG", getString(R.string.not_connected)); 
     return; 
    } 

    try { 
     LocationServices.GeofencingApi.addGeofences(
       mGoogleApiClient, 
       getGeofencingRequest(), 
       getGeofencePendingIntent(this) 
     ).setResultCallback(this); // Result processed in onResult(). 
    } catch (SecurityException securityException) { 
     logSecurityException(securityException); 
    } 
} 

代碼獲取的PendingIntent:

private PendingIntent getGeofencePendingIntent(Context c) { 
    if (mGeofencePendingIntent != null) { 
     return mGeofencePendingIntent; 
    } 
    Intent intent = new Intent(GeofenceService.this, GeofenceTransitionsIntentService.class); 
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

代碼獲取GeofencingRequest:

private GeofencingRequest getGeofencingRequest() { 
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); 
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); 
    builder.addGeofences(mGeofenceList); 
    return builder.build(); 
} 

當用戶進入或退出地理圍欄它不會觸發GeofenceTransitionsIntentService。 它在活動中執行時工作得很好,但不能從服務中運行。

注意:這些函數被定義並稱爲在其中根據用戶的當前位置動態地改變mGeofenceList的服務。

編輯

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.android.routein" > 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

<permission 
    android:name="com.example.gcm.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 



<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    ..... 

    <service android:name=".geofencing.GeofenceTransitionsIntentService" /> 

    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="--My Api Key--" /> 

    ..... 
</application> 

+0

不要緊,你從(活動,服務等)註冊地理圍欄。我想你去通https://developer.android.com/intl/ja/training/location/geofencing.html - 我的作品 - 請你再看一看那些步驟,並檢查你的代碼。 –

+0

請張貼您的清單。 –

+0

@MarianPaździoch您是否使用服務測試了它?即使應用程序不在後臺,Geofence列表是否正在使用新的地理柵欄進行更新? 我的目的是從Places API所得到附近的地點的詳細信息,然後使用時用戶不論改變應用程序的位置,背景色是數據更新地理圍欄列表每次。 –

回答

0

繼例如:https://developer.android.com/training/location/geofencing.html與代碼:https://github.com/googlesamples/android-play-location/tree/master/Geofencing

用途:

mGoogleApiClient.blockingConnect(TIME_OUT, TimeUnit.MILLISECONDS); 

並添加/更改代碼如下:

public class RegisterGeoIntentService extends IntentService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> { 

protected static final String TAG = "RegisterGeoIS"; 

private static final long TIME_OUT = 100; 
protected GoogleApiClient mGoogleApiClient; 
protected ArrayList<Geofence> mGeofenceList; 
private PendingIntent mGeofencePendingIntent; 

public RegisterGeoIntentService() { 
    super(TAG); 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.i(TAG, "Creating Intent service"); 
    mGeofenceList = new ArrayList<Geofence>(); 
    mGeofencePendingIntent = null; 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    buildGoogleApiClient(); 
    populateGeofenceList(); 
    mGoogleApiClient.blockingConnect(TIME_OUT, TimeUnit.MILLISECONDS); 
    String connected = mGoogleApiClient.isConnected() ? "connected" : "disconnected"; 
    Log.i(TAG, "Restoring geofence - status: " + connected); 
    addGeofencesButtonHandler(); 
} 

... 

public void addGeofencesButtonHandler() { 
    if (!mGoogleApiClient.isConnected()) { 
     Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show(); 
     return; 
    } 

    try { 
     LocationServices.GeofencingApi.addGeofences(
       mGoogleApiClient, 
       // The GeofenceRequest object. 
       getGeofencingRequest(), 
       // A pending intent that that is reused when calling removeGeofences(). This 
       // pending intent is used to generate an intent when a matched geofence 
       // transition is observed. 
       getGeofencePendingIntent() 
     ).await(TIME_OUT, TimeUnit.MILLISECONDS); 
    } catch (SecurityException securityException) { 
     // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission. 
     logSecurityException(securityException); 
    } 
    Log.i(TAG, "Trying to add Geofences - result: " + result.toString()); 
} 

... 

添加在AndroidManifest如下:

<service android:name=".RegisterGeoIntentService" /> 

與調用它:

Intent kickoff = new Intent(context, RegisterGeoIntentService.class); 
    context.startService(kickoff);