0
我是一個android新手,學習android,我正在開發一個簡單的Geofencing應用程序。 當我插入單個GeoFence時,一切正常。但是,當我使用2個以上的地理柵欄時,所有柵欄的onReceive都會被觸發,並且所有地理柵欄的通知都會在另一個上面創建。我試圖搜索網頁,並沒有提出任何解決方案。任何人都可以幫我解決我在做什麼錯了嗎? 並且非常感謝。 :)廣播接收器的onReceive()被調用以接收所有的接近警報。我究竟做錯了什麼?
這裏是我寫爲服務類的代碼:
package com.example.multigeofence;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
public class GeoFenceService extends Service {
LocationManager lm;
BroadcastReceiver br;
SQLiteDatabase myDatabase;
BufferedReader bur;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@SuppressLint("SdCardPath")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1,
new MyLocationListener());
addProximityAlert(12.955299,77.679977,"Tata Elxsi",50);
addProximityAlert(12.187497,76.852970,"Mall",50);
addProximityAlert(12.552929,77.079977,"Dummy place",50);
return START_NOT_STICKY;
}
private void addProximityAlert(double latitude, double longitude,
String name, int rad) {
Intent intent = new Intent("idontknowyvusethis");
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0,
intent, 0);
lm.addProximityAlert(latitude, longitude, rad, -1, proximityIntent);
// will be used to generate an Intent to fire when entry to or exit from
// the alert region is detected
IntentFilter filter = new IntentFilter("idontknowyvusethis");
registerReceiver(new proximityIntentReceiver(name, rad), filter);
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
}
}
下面是接收器類的代碼:
package com.example.multigeofence;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
public class proximityIntentReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
private String name;
private int rad;
public proximityIntentReceiver(String name, int rad) {
this.name = name;
this.rad = rad;
}
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
String abc = "";
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
abc = "You are entering " + name + "! Beware!";
Toast.makeText(context, "inside if : " + name, Toast.LENGTH_SHORT).show();
Log.i("GeoGeo", "inside if : " + name);
} else
abc = "You are exiting " + name + "! Plz com bak :(";
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent i = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
i, 0);
Notification notification = createNotification();
notification.setLatestEventInfo(context, abc, "The radius is: "
+ rad, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
private Notification createNotification() {
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;
return notification;
}
}
這裏是清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.multigeofence"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
<service
android:name=".GeoFenceService"
android:enabled="true" />
</application>
</manifest>
任何提示或幫助將不勝感激。謝謝:)
u能張貼表現? – KOTIOS 2014-09-01 12:30:04
@DIVA感謝您的回覆。我用清單編輯了這個問題。 :) – Sunil 2014-09-01 12:34:34
你正在接到多次接聽電話的權利? – KOTIOS 2014-09-01 12:38:13