Android addProximityAlert不会触发

时间:2010-12-16 15:21:41

标签: android

我尝试实现addProximityAlert方法,但它没有触发。这是我的代码:

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清单

<?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> 

抱歉,我没有选择完整的代码。

1 个答案:

答案 0 :(得分:0)

当您添加ProximityAlert时,您必须将PendingIntent传递给LocationManager,这些PendingIntent将在broadcastIntent上触发,我在您的代码中没有看到您为PendingIntent注册接收器的时间。

现在有一个问题......如果你在活动中添加ProximityAlert,为什么要编写服务呢?