Click here to Skip to main content
15,891,863 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I am trying to create a simple geofence and trying to sysout when I am entrying or exiting the geo fence. I have tried a simple positive scenario and using the geofencing mock app from android developer site but I am not getting any responses.

My main activity looks like this..

Java
public class MainActivity extends ActionBarActivity implements
    	GooglePlayServicesClient.ConnectionCallbacks,
    	GooglePlayServicesClient.OnConnectionFailedListener, LocationListener,
    	com.google.android.gms.location.LocationListener,
    	LocationClient.OnAddGeofencesResultListener {
    
    private LocationClient locationClient;
    
    private ArrayList<Geofence> geoFenceList;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	
    	geoFenceList = new ArrayList<Geofence>();
    	locationClient = new LocationClient(this, this, this);
    
    	locationClient.connect();
    	
    
    }
    
    @Override
    public void onConnected(Bundle connectionHint) {
    	
    	
    	Geofence geofence = new Geofence.Builder().setRequestId("1")
    			.setCircularRegion(29.569332, 98.591356, 100)
    			.setExpirationDuration(Geofence.NEVER_EXPIRE)
    			.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER).build();
    
    	geoFenceList.add(geofence);
    
    
    	locationClient.addGeofences(geoFenceList, createPendingIntent(), this);
    	Toast.makeText(this, "Location client connected", Toast.LENGTH_SHORT).show();
    
    }
    
    private PendingIntent createPendingIntent() {
    
    	Intent intent = new Intent(this,
    			ReceiveGeofenceTransitionIntentService.class);
    	//Intent intent1 = new Intent("com.aol.android.geofence.ACTION_RECEIVE_GEOFENCE");
    	return PendingIntent.getService(this, 0, intent,
    			PendingIntent.FLAG_UPDATE_CURRENT);
    	
    
    }
    @Override
    public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) {
    	// TODO Auto-generated method stub
    
    }
    
    @Override
    public void onLocationChanged(Location arg0) {
    	// TODO Auto-generated method stub
    
    }
    
    @Override
    public void onProviderDisabled(String arg0) {
    	// TODO Auto-generated method stub
    
    }
    
    @Override
    public void onProviderEnabled(String arg0) {
    	// TODO Auto-generated method stub
    
    }
    
    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    	// TODO Auto-generated method stub
    
    }
    
    @Override
    public void onConnectionFailed(ConnectionResult result) {}
    
    @Override
    public void onDisconnected() {
    	// TODO Auto-generated method stub
    
    }
         }

Intent service class ....
Java
public class ReceiveGeofenceTransitionIntentService extends IntentService {

             public ReceiveGeofenceTransitionIntentService() {
        	super("ReceiveGeofenceTransitionsIntentService");
        
        }
    
    @Override
    protected void onHandleIntent(Intent intent) {
    
    	// Create a local broadcast Intent
    
    	Intent broadcastIntent = new Intent();
    
    	// Give it the category for all intents sent by the Intent Service
    
    	broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);
    
    	
    		// Get the type of transition (entry or exit)
    
    		int transition = LocationClient.getGeofenceTransition(intent);
    
    		if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER) ||
    
    		(transition == Geofence.GEOFENCE_TRANSITION_EXIT))
    
    		System.out.println("Geofence Transition occured!!!!!!!!!!");
    			Toast.makeText(this, "Geofence Transition occured",      Toast.LENGTH_SHORT).show();
    		{
    
    		}
    	}
}

I am not able to understand where I am going wrong. I am giving two locations in the mock geofence app one is around which I am building the geofence and other pretty far away.I could see the transition of my device in google to and fro between those two locations but I am not getting any transition notification from my application.

My manifest goes like this....

JavaScript
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.ocrvin"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            
            <meta-data
                
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version"
                
                />
            <service
                android:name="com.aol.android.geofence.ReceiveTransitionsIntentService"
                android:exported="false" >
            </service>
            
            <intent-filter >
                <action android:name="com.aol.android.geofence.ACTION_RECEIVE_GEOFENCE"/>
            </intent-filter>
        </receiver>
            <activity
                android:name="com.prototype.ocrvin.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
       <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    </manifest>


Please help me as I am stuck in this for many days.
Posted
Updated 3-Jun-14 7:11am
v2
Comments
ridoy 1-Dec-16 4:38am    
Did you test it in a real device? If yes, then try once again after restarting your device for once.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900