2010-12-16 104 views
1

我试图执行addProximityAlert方法,但它不会触发。这里是我的代码: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> 

对不起,我选择了不完整的代码。

+0

你解决了吗? – powder366 2013-07-21 14:24:21

回答

0

当您添加一个ProximityAlert时,您必须将PendingIntent传递给LocationManager,这些PendingIntent将在broadcastIntent上触发,并且在您为其注册PendingIntent的接收器时,在代码中看不到您的代码。

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