您好我正在尝试在 Android 中添加地理围栏的功能。我正在使用http://developer.android.com/training/location/geofencing.html创建和监控地理围栏。我正在使用IntentService
警报(进入/退出),但对我来说它不起作用。
但是当我离开并回到该地区进行测试时,它对我不起作用。我已打开设备的 GPS,但设备未连接到互联网。
任何人都可以请帮助我使它更准确和完美,没有任何问题。
public class MainActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationClient.OnAddGeofencesResultListener {
private LocationClient locationClient;
private String TAG = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resp == ConnectionResult.SUCCESS) {
locationClient = new LocationClient(this, this, this);
locationClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
ArrayList<Store> storeList = getStoreList();
if (null != storeList && storeList.size() > 0) {
ArrayList<Geofence> geofenceList = new ArrayList<Geofence>();
for (Store store : storeList) {
float radius = (float) store.radius;
Geofence geofence = new Geofence.Builder()
.setRequestId(store.id)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.setCircularRegion(store.latitude, store.longitude, radius)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
geofenceList.add(geofence);
}
PendingIntent geoFencePendingIntent = PendingIntent.getService(this, 0,
new Intent(this, GeofenceIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
locationClient.addGeofences(geofenceList, geoFencePendingIntent, this);
}
}
@Override
public void onDisconnected() {
Log.e(TAG, "Disconnected !");
}
@Override
public void onAddGeofencesResult(int i, String[] strings) {
if (LocationStatusCodes.SUCCESS == i) {
//todo check geofence status
} else {
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, connectionResult.getErrorCode() + "");
}
private ArrayList<Store> getStoreList() {
ArrayList<Store> storeList = new ArrayList<Store>();
for (int i = 0; i < 1; i++) {
Store store = new Store();
store.id = String.valueOf(i);
store.address = "India";
store.latitude = 26.7802187;
store.longitude = 75.860322;
store.radius = 100.0;
storeList.add(store);
}
return storeList;
}
public class Store {
String id;
String address;
double latitude;
double longitude;
double radius;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != locationClient) {
locationClient.disconnect();
}
}
}
GeofenceIntentService.java
public static final String TRANSITION_INTENT_SERVICE = "ReceiveTransitionsIntentService";
public GeofenceIntentService() {
super(TRANSITION_INTENT_SERVICE);
}
@Override
protected void onHandleIntent(Intent intent) {
if (LocationClient.hasError(intent)) {
//todo error process
} else {
int transitionType = LocationClient.getGeofenceTransition(intent);
if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ||
transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
List<Geofence> triggerList = LocationClient.getTriggeringGeofences(intent);
for (Geofence geofence : triggerList) {
generateNotification(geofence.getRequestId(), "address you defined");
}
}
}
}
private void generateNotification(String locationId, String address) {
long when = System.currentTimeMillis();
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.putExtra("id", locationId);
notifyIntent.putExtra("address", address);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.dac_logo)
.setContentTitle(locationId)
.setContentText(address)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setWhen(when);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) when, builder.build());
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<application
android:allowBackup="true"
android:icon="@drawable/dac_logo"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:excludeFromRecents="true"
android:label="@string/app_name"
android:launchMode="singleTask"
android:taskAffinity="" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".GeofenceIntentService"
android:exported="false" />
<receiver android:name="com.location.demo.receivers.BootCompleteReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>