簡體   English   中英

即使應用被殺死,如何繼續接收地理圍欄通知?

[英]How to receive continuous geofence notifications even when app is killed?

我在我的應用程序中使用了最新的geofence api,並按照Google的示例代碼來構建該應用程序。 但是當應用程序被殺死時它不起作用。我必須再次單擊添加地理圍欄按鈕以觸發地理圍欄。我希望可以始終觸發地理圍欄。 我該怎么辦? 這是我的代碼:

主要活動

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks
    , GoogleApiClient.OnConnectionFailedListener,ResultCallback<Status> {
protected GoogleApiClient googleApiClient;
protected ArrayList<Geofence> geofenceArrayList;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button= (Button) findViewById(R.id.button);
    geofenceArrayList = new ArrayList<>();
    populateGeofenceList();
    buildGoogleApiClient();
}

protected synchronized void buildGoogleApiClient() {
    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    if (!googleApiClient.isConnected() || !googleApiClient.isConnecting()) {
        googleApiClient.connect();
    }
}

@Override
protected void onStop() {
    super.onStop();
    if (googleApiClient.isConnected() || googleApiClient.isConnecting()) {
        googleApiClient.disconnect();
    }
}

public void populateGeofenceList() {
    for (Map.Entry<String, LatLng> entry : Constants.MY_LANDMARKS.entrySet()) {
        geofenceArrayList.add(new Geofence.Builder()
                .setRequestId(entry.getKey())
                .setCircularRegion(entry.getValue().latitude, entry.getValue().longitude, 100)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_DWELL | Geofence.GEOFENCE_TRANSITION_EXIT)
                .setLoiteringDelay(300000)
                .build());
    }
}
private GeofencingRequest getGeofencingRequest(){
    GeofencingRequest.Builder builder=new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
            .addGeofences(geofenceArrayList);
    return builder.build();
}
private PendingIntent getGeofencePendingIntent(){
    Intent intent=new Intent(this,GeofenceTransitionService.class);
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public void addGeofencesButtonHandler(View view){
    if(!googleApiClient.isConnected()){
        Toast.makeText(this,"Client not connected",Toast.LENGTH_SHORT).show();
        return;
    }
    try {
        LocationServices.GeofencingApi.addGeofences(googleApiClient,getGeofencingRequest(),getGeofencePendingIntent()).setResultCallback(this);
    }catch (SecurityException securityException){

    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {
    googleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onResult(Status status) {
    if(status.isSuccess()){
        Toast.makeText(this,"Geofence added",Toast.LENGTH_SHORT).show();
    }
    else {
        String errorMessage=GeofenceErrorMessages.getErrorString(this,status.getStatusCode());
        Log.e("MainActivity",errorMessage);
    }
}

}

意向服務

public class GeofenceTransitionService extends IntentService {
/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 *
 * @param name Used to name the worker thread, important only for debugging.
 */
public static final String TAG="GeofenceService";

@Override
public void onCreate() {
    super.onCreate();
}

public GeofenceTransitionService() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent=GeofencingEvent.fromIntent(intent);
    if(geofencingEvent.hasError()){
        String errorMsg=GeofenceErrorMessages.getErrorString(this,geofencingEvent.getErrorCode());
        Log.e(TAG,errorMsg);
        return;
    }
    int geofenceTransition=geofencingEvent.getGeofenceTransition();
    if(geofenceTransition== Geofence.GEOFENCE_TRANSITION_ENTER||geofenceTransition==Geofence.GEOFENCE_TRANSITION_DWELL
            ||geofenceTransition==Geofence.GEOFENCE_TRANSITION_EXIT){
        List<Geofence> triggeringGeofences=geofencingEvent.getTriggeringGeofences();
        String geofenceTransitionDetails=getGeofenceTransiionDetails(this,geofenceTransition,triggeringGeofences);
        sendNotification(geofenceTransitionDetails);
        Log.i(TAG,geofenceTransitionDetails);
    }
    else {
        Log.e(TAG,"Invalid Transition : "+geofenceTransition);
    }
}
private String getGeofenceTransiionDetails(Context context,int transition,List<Geofence> triggeringGeofences){
    String geofenceTransitionString=getTransitionString(transition);
    ArrayList GeofenceIdsList=new ArrayList();
    for(Geofence geofence:triggeringGeofences){
        GeofenceIdsList.add(geofence.getRequestId());
    }
    String geofencesIdsString= TextUtils.join(", ", GeofenceIdsList);
    return geofenceTransitionString+": "+geofencesIdsString;
}
private String getTransitionString(int transitionType){
    switch (transitionType){
        case Geofence.GEOFENCE_TRANSITION_ENTER:
            return "Entered the geofence";
        case Geofence.GEOFENCE_TRANSITION_DWELL:
            return "Dwelling in the geofence";
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            return "Exited the geofence";
        default:return "Unknown Transition";
    }
}
private void sendNotification(String notificationDetails){
    Intent notificationIntent=new Intent(getApplicationContext(),MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(notificationIntent);
    PendingIntent notificationPendingIntent=stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(notificationDetails)
            .setContentText("Click to return to app")
            .setContentIntent(notificationPendingIntent);
    builder.setAutoCancel(true);
    NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());
}

}

django,您的問題已經在這里得到解答。

https://stackoverflow.com/a/19521823/5320593

您應該使用BroadcastReceiver而不是IntentService或相應地使用它們以避免ANR。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM