2013-05-28 14 views
6

मैं इस वर्ष में जारी भूगर्भ के साथ एंड्रॉइड ऐप विकसित कर रहा हूं।google play geofence onHandleIntent

मेरे समझ जब उन में (या बाहर) बाड़ इसे हालांकि विधि onHandleIntent को गति प्रदान करना चाहिए, मैं onHandleIntent विधि को गति प्रदान करने के कठिनाई होती है

मैं 3 दिनों के लिए जाँच की है यह पता लगा, लेकिन मैं करने के लिए evetually नहीं कर सका।

इसलिए मुझे किसी की मदद चाहिए।

मेरा कोड है, मुझे उम्मीद है कि कोई भी मेरी मदद कर सकता है।

///// MainActivity 

    package com.example.geofence; 


    import java.util.ArrayList; 
    import java.util.List; 

    import android.app.PendingIntent; 
    import android.content.BroadcastReceiver; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.IntentFilter; 
    import android.os.Bundle; 
    import android.support.v4.app.FragmentActivity; 
    import android.support.v4.content.LocalBroadcastManager; 
    import android.text.TextUtils; 
    import android.util.Log; 

    import android.widget.Toast; 

    import com.google.android.gms.common.ConnectionResult; 
    import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks; 
    import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener; 
    import com.google.android.gms.common.GooglePlayServicesUtil; 
    import com.google.android.gms.location.Geofence; 
    import com.google.android.gms.location.LocationClient; 
    import com.google.android.gms.location.LocationClient.OnAddGeofencesResultListener; 

    import com.google.android.gms.location.LocationRequest; 
    import com.google.android.gms.location.LocationStatusCodes; 

    public class MainActivity extends FragmentActivity implements 
             ConnectionCallbacks, 
             OnConnectionFailedListener, 
             OnAddGeofencesResultListener 
             { 
    private IntentFilter mIntentFilter; 
    private LocationClient locationClient; 
    private LocationRequest locatRequest; 

    private PendingIntent intent; 
    private List<Geofence> mGeoList; 
    private Context mContext; 
    private Geofence companyLocation; 
    private GeofenceSampleReceiver mBroadcastReceiver; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mContext = this; 

     locatRequest = null; 
     mGeoList = new ArrayList<Geofence>(); 

     intent = null; 
     locationClient = new LocationClient(this,this,this); 

     mIntentFilter = new IntentFilter(); 
     mIntentFilter.addAction("com.example.geofence.ACTION_GEOFENCES_ADDED"); 
     mIntentFilter.addCategory("com.example.geofence.CATEGORY_LOCATION_SERVICES"); 
     mBroadcastReceiver = new GeofenceSampleReceiver(); 
    } 




    @Override 
    protected void onStart() { 

     companyLocation = new Geofence.Builder() 
     .setRequestId("1") 
     .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) 
     .setCircularRegion(
       49.220531, -122.986772, (float)50) 
     .setExpirationDuration(Geofence.NEVER_EXPIRE) 
     .build(); 

     mGeoList.add(companyLocation); 
     locationClient.connect(); 
     super.onStart();  

    } 



    @Override 
    public void onConnected(Bundle arg0) { 
     // TODO Auto-generated method stub 
     intent = getTransitionPendingIntent(); 

     locatRequest = LocationRequest.create(); 
      locatRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
      locatRequest.setInterval(5000); 

      try{ 
       addGeofence(); 
      }catch(UnsupportedOperationException e){ 
      Toast.makeText(this, "add_geofences_already_requested_error", 
       Toast.LENGTH_LONG).show(); 
      } 
     // locationClient.requestLocationUpdates(locatRequest, intent); 


    } 

    public void addGeofence(){ 
     locationClient.addGeofences(mGeoList,intent , this); 

    } 

    private PendingIntent getTransitionPendingIntent() { 
      // Create an explicit Intent 
      Intent localIntent = new Intent(this, 
        ReceiveTransitionsIntentService.class); 
      /* 
      * Return the PendingIntent 
      */ 
      return PendingIntent.getService(
        this, 
        0, 
        localIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 
    } 






    @Override 
    protected void onStop() { 

      locationClient.disconnect(); 
      super.onStop(); 

    } 

    @Override 
    public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) { 
     // TODO Auto-generated method stub 
     Intent broadcastIntent = new Intent(); 

     if(LocationStatusCodes.SUCCESS == statusCode){ 
      Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show(); 

    broadcastIntent.setAction("com.example.android.geofence.ACTION_GEOFENCES_ADDED") 
          .addCategory("com.example.android.geofence.CATEGORY_LOCATION_SERVICES") 
            .putExtra("com.example.android.geofence.EXTRA_GEOFENCE_STATUS","test"); 
     } 
     else{ 
      Toast.makeText(this, "AddGeoError", Toast.LENGTH_SHORT).show(); 
     } 

     LocalBroadcastManager.getInstance(mContext).sendBroadcast(broadcastIntent); 

    } 



    @Override 
    public void onConnectionFailed(ConnectionResult arg0) {  // TODO Auto-generated method stub 
     int code = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     switch(code){ 

       case ConnectionResult.SERVICE_MISSING :{ 
        Toast.makeText(this, "SERVICE_MISSING " + code + " ConnectionResult.SERVICE_MISSING " +ConnectionResult.SERVICE_MISSING, Toast.LENGTH_SHORT).show(); 
        break; 

       } 
       case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:{ 

        Toast.makeText(this, "SERVICE_VERSION_UPDATE_REQUIRED " + code + " ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED " + ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED, Toast.LENGTH_SHORT).show(); 

        break; 
       } 
       default:{ 
        Toast.makeText(this, "start " + code, Toast.LENGTH_SHORT).show(); 
       } 

      } 
    } 



    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
    } 



    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
    } 



    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, mIntentFilter); 
     //locationClient.connect(); 
     super.onResume(); 
    } 


    @Override 
    public void onDisconnected() { 
     // TODO Auto-generated method stub 
     locationClient = null; 
    } 

    /* 
    * Handle results returned to this Activity by other Activities started with 
    * startActivityForResult(). In particular, the method onConnectionFailed() in 
    * GeofenceRemover and GeofenceRequester may call startResolutionForResult() to 
    * start an Activity that handles Google Play services problems. The result of this 
    * call returns here, to onActivityResult. 
    * calls 
    */ 

    @Override 
    protected void onActivityResult(
       int requestCode, int resultCode, Intent data) { 
    } 




    /** 
    * Define a Broadcast receiver that receives updates from connection listeners and 
    * the geofence transition service. 
    */ 
    public class GeofenceSampleReceiver extends BroadcastReceiver { 
     /* 
     * Define the required method for broadcast receivers 
     * This method is invoked when a broadcast Intent triggers the receiver 
     */ 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      // Check the action code and determine what to do 
      String action = intent.getAction(); 

      // Intent contains information about errors in adding or removing geofences 
     if (TextUtils.equals(action, "com.example.geofence.ACTION_GEOFENCES_ADDED")) { 

       handleGeofenceStatus(context, intent); 

      // Intent contains information about a geofence transition 
      } else if (TextUtils.equals(action, "com.example.geofence.ACTION_GEOFENCE_TRANSITION")) { 

       handleGeofenceTransition(context, intent); 

      // The Intent contained an invalid action 
      } else { 

       Toast.makeText(context,"error", Toast.LENGTH_LONG).show(); 
      } 
     } 

     /** 
     * If you want to display a UI message about adding or removing geofences, put it here. 
     * 
     * @param context A Context for this component 
     * @param intent The received broadcast Intent 
     */ 
     private void handleGeofenceStatus(Context context, Intent intent) { 

     } 

     /** 
     * Report geofence transitions to the UI 
     * 
     * @param context A Context for this component 
     * @param intent The Intent containing the transition 
     */ 
     private void handleGeofenceTransition(Context context, Intent intent) { 
      /* 
      * If you want to change the UI when a transition occurs, put the code 
      * here. The current design of the app uses a notification to inform the 
      * user that a transition has occurred. 
      */ 
     } 

     /** 
     * Report addition or removal errors to the UI, using a Toast 
     * 
     * @param intent A broadcast Intent sent by ReceiveTransitionsIntentService 
     */ 
     private void handleGeofenceError(Context context, Intent intent) { 

     } 
    } 


} 



///// ReceiveTransitionsIntentService 

    package com.example.geofence; 

    import java.util.List; 

    import android.app.IntentService; 
    import android.app.NotificationManager; 
    import android.app.PendingIntent; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.support.v4.app.NotificationCompat; 
    import android.support.v4.app.TaskStackBuilder; 
    import android.util.Log; 
    import android.widget.Toast; 

    import com.google.android.gms.location.Geofence; 
    import com.google.android.gms.location.LocationClient; 

    public class ReceiveTransitionsIntentService extends IntentService { 

    /** 
    * Sets an identifier for the service 
    */ 
    private Context mContext; 
    public ReceiveTransitionsIntentService(Context c) { 

     super("ReceiveTransitionsIntentService"); 
     mContext =c; 
    } 

    /** 
    * Handles incoming intents 
    *@param intent The Intent sent by Location Services. This 
    * Intent is provided 
    * to Location Services (inside a PendingIntent) when you call 
    * addGeofences() 
    */ 
    @Override 
    protected void onHandleIntent(Intent intent) { 
     // First check for errors 

     //Toast.makeText(mContext, "onHandleIntent", Toast.LENGTH_LONG).show(); 
     if (LocationClient.hasError(intent)) { 
      // Get the error code with a static method 
      int errorCode = LocationClient.getErrorCode(intent); 
      // Log the error 
      Log.e("ReceiveTransitionsIntentService", 
        "Location Services error: " + 
        Integer.toString(errorCode)); 
      /* 
      * You can also send the error code to an Activity or 
      * Fragment with a broadcast Intent 
      */ 
     /* 
     * If there's no error, get the transition type and the IDs 
     * of the geofence or geofences that triggered the transition 
     */ 
     } else { 


       int transitionType = LocationClient.getGeofenceTransition(intent); 
      if(transitionType == Geofence.GEOFENCE_TRANSITION_ENTER || 
        transitionType == Geofence.GEOFENCE_TRANSITION_EXIT){ 
       List<Geofence> geofences = LocationClient.getTriggeringGeofences(intent); 
       String[] geofenceIds = new String [geofences.size()]; 
       for(int i = 0; i <geofences.size(); i++){ 
        geofenceIds[i] = geofences.get(i).getRequestId(); 
       } 
       String ids = "1"; 
       String transition = ((transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) ?"you are in":"you are out"); 
       Toast.makeText(mContext, transition, Toast.LENGTH_LONG).show(); 

       sendNotification(transition,ids); 
       //FOR THE NOTIFICATION. 

       //NotificationManager mNotificationManager = 
        //  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

      } 




     } 
    } 




    private void sendNotification(String transitionType, String ids) { 

     // Create an explicit content Intent that starts the main Activity 
     Intent notificationIntent = 
       new Intent(getApplicationContext(),MainActivity.class); 

     // Construct a task stack 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 

     // Adds the main Activity to the task stack as the parent 
     stackBuilder.addParentStack(MainActivity.class); 

     // Push the content Intent onto the stack 
     stackBuilder.addNextIntent(notificationIntent); 

     // Get a PendingIntent containing the entire back stack 
     PendingIntent notificationPendingIntent = 
       stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

     // Get a notification builder that's compatible with platform versions >= 4 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

     // Set the notification contents 
     builder.setSmallIcon(R.drawable.ic_notification) 
       .setContentTitle(
         transitionType+ " geofence_transition_notification_title " +ids) 
       .setContentIntent(notificationPendingIntent); 

     // Get an instance of the Notification manager 
     NotificationManager mNotificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     // Issue the notification 
     mNotificationManager.notify(0, builder.build()); 
    } 



} 
+0

और मेरे मैनिफ़ पर सेंट मेरे पास है- सेवा एंड्रॉइड: निर्यात किया गया = "झूठा" एंड्रॉइड: नाम = "com.example.geofence.ReceiveTransitionsIntentService" और उपयोग-अनुमति एंड्रॉइड: name = "android.permission.ACCESS_FINE_LOCATION" – user1979727

+0

आपने अपना संपूर्ण गतिविधि कोड क्यों पोस्ट किया ? यह पढ़ने के लिए बहुत अधिक है। बस हमें प्रासंगिक समस्या स्निपेट पर इंगित करें। –

उत्तर

0

आपका त्रिज्या 50 पर सेट है जिसका मतलब है कि आपको हैंडल ट्रिगर करने के लिए अपने स्थान से 50 मीटर की यात्रा करने की आवश्यकता है। क्या आपने 2 एम त्रिज्या का उपयोग करने की कोशिश की है?

+0

हां, मैंने 2, 5, 10, 1000 मीटर जैसे विभिन्न राडुओं के साथ इतने सारे परीक्षण किए हैं। लेकिन यह सही ढंग से कभी नहीं चलाता है। – user1979727

+0

मुझे एक ही समस्या थी और दो दिनों के बाद यह काम करना शुरू कर दिया ... संक्रमण में पंजीकरण करने में काफी समय लग रहा है। कुछ चीजें; यह जांचें कि फोन की सेटिंग्स-> ऐप्स-> चल रहा है पर IntentService चल रहा है। कहीं कहीं अतिरिक्त Google सेवा होनी चाहिए। ऐडेंट सेवा से ऐडेंट सेवा को स्थापित करने और प्रसारण रिसीवर में कुछ आउटपुट करने का प्रयास करने के लिए सब कुछ ठीक से हुकिंग की जांच करने का प्रयास करें। यदि सबकुछ ठीक है, तो हो सकता है कि आपका ऐप रोकें/रोकें, ठीक से रोकें, इसलिए बाड़ छोड़ने के 5 मिनट बाद अपने फोन को ऐप के साथ खोलें। – Njall

-1
return PendingIntent.getService(
        this, 
        0, 
        localIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 

बदलना चाहिए करने के लिए:

return PendingIntent.getBroadcast(
        this, 
        0, 
        localIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 
0

अपने OnCreate() विधि में, आप GoogleApiClient का दृष्टांत चाहिए:

mApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 

    mApiClient.connect(); 
0

समस्या आप geofence निगरानी करने के लिए एक LocationRequest की जरूरत है स्थान ...

यदि आप google ma का उपयोग करते हैं एक ही समय में पीएस। आपकी भूगर्भ निगरानी काम करेगी! क्योंकि गूगल मैप्स LocationRequest बनाने के लिए और geofence इसे देख ...

तो अगर आप की निगरानी यदि आप एक LocationRequest हैंडलर बनाने की जरूरत है geofence में स्वायत्त होना जरूरत है ...

Google Api LocationRequest

उदाहरण कोड

FusedLocationProviderClient fusedLocationProviderClient = new FusedLocationProviderClient(activity); 
    fusedLocationProviderClient.requestLocationUpdates(getLocationRequest(), getPendingIntent()); 


private LocationRequest getLocationRequest() { 
    LocationRequest mLocationRequest = new LocationRequest(); 

    mLocationRequest.setInterval(10000); 
    mLocationRequest.setFastestInterval(5000); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    return mLocationRequest; 
} 

private PendingIntent getPendingIntent() { 
    // Reuse the PendingIntent if we already have it. 
    Log.e(TAG, "Creating Monitoring Geofence..."); 
    if (mGeofencePendingIntent != null) { 
     return mGeofencePendingIntent; 
    } 

    Intent intent = new Intent(activity, LocationService.class); 
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when 
    // calling addGeofencesTask() and removeGeofences(). 
    mGeofencePendingIntent = PendingIntent.getService(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    return mGeofencePendingIntent; 
} 

इस कोड में आप 10 सेकंड पर एक स्थान अनुरोध लेते हैं ... और भौगोलिकता प्रत्येक 10 सेकंड भौगोलिक क्षेत्र की निगरानी ...