开发者

Android addProximityAlert does not fire

I tried to implement the addProximityAlert method, but it does not fire. Here is my code:

SampleProximityAlert

public class SampleProximityAlert extends Activity {
 /** Called when the activity is first created. */

 protected LocationManager locationManager;
 protected Location location;
 public static Location pLocation;
 protected Intent intent;
 protected PendingIntent pIntent;
 protected GeoPoint point;
 public float distance;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  locationManager = (LocationManager) this
    .getSystemService(Context.LOCATION_SERVICE);
  location = locationManager
    .getLastKnownLocation(LocationManager.GPS_PROVIDER);

  try {
   locationManager.requestLocationUpdates(
     LocationManager.GPS_PROVIDER, 1000, 1,
     new ProximityListener());
  } catch (RuntimeException e) {
   e.printStackTrace();
  }
  // point = new GeoPoint(49868004, 8626971);
  try {
   pLocation = new Location(location.getProvider());
  } catch (RuntimeException e) {
   e.printStackTrace();
  }
  Log.i("PROVIDER", location.getProvider());

  pLocation.setLatitude(49.868004);
  pLocation.setLongitude(8.626971);

  distance = methodeDistance(pLocation);

  Log.i("DISTANCE", String.valueOf(distance));

  Log.i("POSITION",
    String.valueOf(location.getLatitude()) + ","
      + String.valueOf(location.getLongitude()));
  intent = new Intent(this, ProximityAlert.class);
  intent.setAction("ProximityAlert");
  pIntent = PendingIntent.getService(this, 0, intent, 0);

  locationManager.addProximityAlert(49.868004, 8.626971, 429, 50000,
    pIntent);
 }

 public float methodeDistance(Location mLocation) {
  float mDistance = location.distanceTo(pLocation);
  return mDistance;
 }
}

ProximityAlert

public class ProximityAlert extends Service {
 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  Log.i("Alert", "fired");
  return null;
 }

 @Override
 public void onCreate() {
  super.onCreate();
  Log.i("ONCREATE", "create ProximityAlert as Service");
 }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  Log.i("ONSTARTCOMMAND", "OnStartCommand");
  return super.onStartCommand(intent, flags, startId);
 }
}

ProximityListener

public class ProximityListener implements LocationListener {
 String DEBUG_TAG = "ProximityListener";

 @Override
 public void onLocationChanged(Location location) {
  // TODO Auto-generated method stub
  Log.d(DEBUG_TAG, location.toString());
  // tvD.setText(String.valueOf(location.distanceTo(pLocation)));
 }

 @Override
 public void onProviderDisabled(String provider) {
  // TODO Auto-generated method stub
 }

 @Override
 public void onProviderEnabled(String provider) {
  // TODO Auto-generated method stub
 }

 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {
  // TODO Auto-generated method stub
 }
}

XML Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.tsystems.proximityAlert"
      android:versionCode="1"
      android:versionName="1.0">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SampleProximityAlert"
                  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=".ProximityAlert">
            <intent-filter>
                <action android:name="ProximityAlert"/>
                <category android:name="com.proaktiveOrtung"/>
            </intent-filter>
        </service>
        <service android:name=".ProximityAlert"></service>
    </application>
    <uses-sdk android:minSdkVersion="8" />
</manifest>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.tsystems.proximityAlert"
      android:versionCode="1"
      android:versionName="1.0">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SampleProximityAlert"
                  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=".ProximityAlert">
            <intent-filter>
                <action android:name="ProximityAlert"/>
                <category android:name="com.proaktiveOrtung"/>
            </intent-filter>
        </service>
        <service android:name=".ProximityAlert"></service>
    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest> 

Sorry I selected not the complete code.


when you add a ProximityAlert you have to pass a PendingIntent to the LocationManager, these PendingIntent will be fired on a broadcastIntent and i dont see in your code when, where you register a receiver for that PendingIntent.

Now a question... why do you write a service if you add the ProximityAlert on an Activity?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜