2
2
3
3
import android .app .Activity ;
4
4
import android .app .PendingIntent ;
5
- import android .content .BroadcastReceiver ;
6
- import android .content .Context ;
7
5
import android .content .Intent ;
8
- import android .content .IntentFilter ;
9
6
import android .location .Location ;
10
7
import android .os .Bundle ;
11
- import android .support .v4 .content .LocalBroadcastManager ;
12
8
import android .widget .CompoundButton ;
13
9
import android .widget .ToggleButton ;
14
10
23
19
24
20
import java .util .ArrayList ;
25
21
26
-
27
22
public class MainActivity extends Activity implements GooglePlayServicesClient .ConnectionCallbacks ,
28
23
GooglePlayServicesClient .OnConnectionFailedListener ,
29
24
LocationClient .OnAddGeofencesResultListener ,
30
25
LocationClient .OnRemoveGeofencesResultListener {
31
26
32
- private static String FENCE_ID = "com.ptrprograms.geofence" ;
27
+ private final static String FENCE_ID = "com.ptrprograms.geofence" ;
28
+ private final int RADIUS = 100 ;
33
29
34
30
private ToggleButton mToggleButton ;
35
31
private Geofence mGeofence ;
36
32
private LocationClient mLocationClient ;
37
33
private Intent mIntent ;
38
34
private PendingIntent mPendingIntent ;
39
- private GeofenceSampleReceiver mBroadcastReceiver ;
40
- IntentFilter mIntentFilter = new IntentFilter ();
41
35
42
36
@ Override
43
37
protected void onCreate (Bundle savedInstanceState ) {
@@ -50,12 +44,10 @@ protected void onCreate(Bundle savedInstanceState) {
50
44
mIntent = new Intent ( this , GeofencingService .class );
51
45
mPendingIntent = PendingIntent .getService ( this , 0 , mIntent , PendingIntent .FLAG_UPDATE_CURRENT );
52
46
53
- mBroadcastReceiver = new GeofenceSampleReceiver ();
54
-
55
47
mToggleButton = (ToggleButton ) findViewById ( R .id .geofencing_button );
56
48
mToggleButton .setOnCheckedChangeListener ( new CompoundButton .OnCheckedChangeListener () {
57
49
@ Override
58
- public void onCheckedChanged (CompoundButton compoundButton , boolean b ) {
50
+ public void onCheckedChanged ( CompoundButton compoundButton , boolean b ) {
59
51
if ( b ) {
60
52
startGeofence ();
61
53
} else {
@@ -81,18 +73,12 @@ private void verifyPlayServices() {
81
73
82
74
private void startGeofence () {
83
75
Location location = mLocationClient .getLastLocation ();
84
- int radius = 100 ;
85
76
86
77
Geofence .Builder builder = new Geofence .Builder ();
87
- mGeofence = builder
88
- .setRequestId (FENCE_ID )
89
- .setCircularRegion (
90
- location .getLatitude (),
91
- location .getLongitude (),
92
- radius )
93
- .setTransitionTypes (Geofence .GEOFENCE_TRANSITION_ENTER
94
- | Geofence .GEOFENCE_TRANSITION_EXIT )
95
- .setExpirationDuration (Geofence .NEVER_EXPIRE )
78
+ mGeofence = builder .setRequestId ( FENCE_ID )
79
+ .setCircularRegion ( location .getLatitude (), location .getLongitude (), RADIUS )
80
+ .setTransitionTypes ( Geofence .GEOFENCE_TRANSITION_ENTER | Geofence .GEOFENCE_TRANSITION_EXIT )
81
+ .setExpirationDuration ( Geofence .NEVER_EXPIRE )
96
82
.build ();
97
83
98
84
ArrayList <Geofence > geofences = new ArrayList <Geofence >();
@@ -110,7 +96,6 @@ protected void onResume() {
110
96
if ( !mLocationClient .isConnected () && !mLocationClient .isConnecting () ) {
111
97
mLocationClient .connect ();
112
98
}
113
- LocalBroadcastManager .getInstance (this ).registerReceiver (mBroadcastReceiver , mIntentFilter );
114
99
}
115
100
116
101
@ Override
@@ -119,13 +104,6 @@ protected void onPause() {
119
104
mLocationClient .disconnect ();
120
105
}
121
106
122
- public class GeofenceSampleReceiver extends BroadcastReceiver {
123
- @ Override
124
- public void onReceive (Context context , Intent intent ) {
125
- startService ( mIntent );
126
- }
127
- }
128
-
129
107
@ Override
130
108
public void onConnected (Bundle bundle ) {
131
109
@@ -140,7 +118,6 @@ public void onDisconnected() {
140
118
public void onAddGeofencesResult (int status , String [] geofenceIds ) {
141
119
if ( status == LocationStatusCodes .SUCCESS ) {
142
120
Intent intent = new Intent ( mIntent );
143
- intent .setAction (GeofencingService .ACTION_INIT );
144
121
startService ( intent );
145
122
}
146
123
}
@@ -153,9 +130,8 @@ public void onConnectionFailed(ConnectionResult connectionResult) {
153
130
@ Override
154
131
public void onRemoveGeofencesByRequestIdsResult (int status , String [] strings ) {
155
132
if ( status == LocationStatusCodes .SUCCESS ) {
133
+ stopService ( mIntent );
156
134
}
157
-
158
- stopService ( mIntent );
159
135
}
160
136
161
137
@ Override
@@ -164,4 +140,5 @@ public void onRemoveGeofencesByPendingIntentResult(int status, PendingIntent pen
164
140
stopService ( mIntent );
165
141
}
166
142
}
143
+
167
144
}
0 commit comments