0

我想向我的位置管理器添加近似警报。我给出纬度和经度,并在距离参数中给出 20。当我从模拟器中给出相同的纬度和经度时,我的近似未决意图不会触发。怎么了?

这是我的 onCreate()

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        lm.addProximityAlert(31.4474, 73.08, 20, -1, PendingIntent.getActivity(
                Main.this, 0, new Intent(android.content.Intent.ACTION_VIEW,
                        Uri.parse("http://www.google.com")), 0));
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}

        @Override
        public void onProviderEnabled(String arg0) {}

        @Override
        public void onProviderDisabled(String arg0) {}

        @Override
        public void onLocationChanged(Location arg0) {
            Toast.makeText(Main.this,arg0.getLatitude()+" "+arg0.getLongitude(), 300).show();
        }
    });
}

清单.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.proximityalert"
    android:versionCode="1"
    android:versionName="1.0" 

>

<uses-sdk
    android:minSdkVersion="6"
    android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.proximityalert.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

4

1 回答 1

0

尝试这个 :

Intent intent = new Intent(PROX_ALERT_INTENT);
        PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        locationManager.addProximityAlert(
            latitude, // the latitude of the central point of the alert region
            longitude, // the longitude of the central point of the alert region
            POINT_RADIUS, // the radius of the central point of the alert region, in meters
            PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration 
            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(PROX_ALERT_INTENT);  
       registerReceiver(new ProximityIntentReceiver(), filter);

ProximityIntentReceiver.java

public class ProximityIntentReceiver extends BroadcastReceiver {

    private static final int NOTIFICATION_ID = 1000;

    @Override
    public void onReceive(Context context, Intent intent) {

        String key = LocationManager.KEY_PROXIMITY_ENTERING;

        Boolean entering = intent.getBooleanExtra(key, false);

        if (entering) {
            Log.d(getClass().getSimpleName(), "entering");
        }
        else {
            Log.d(getClass().getSimpleName(), "exiting");
        }

        NotificationManager notificationManager = 
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);       

        Notification notification = createNotification();
        notification.setLatestEventInfo(context, "Proximity Alert!", "You are near your point of interest.", pendingIntent);

        notificationManager.notify(NOTIFICATION_ID, notification);

    }

    private Notification createNotification() {
        Notification notification = new Notification();

        notification.icon = R.drawable.ic_menu_notifications;
        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;
    }

}

所需权限:

<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" />

private static final String PROX_ALERT_INTENT = "<your pack name>.ProximityAlert";
于 2013-02-01T13:11:41.693 回答