【问题标题】:Android Proximity Alert not workingAndroid 接近警报不起作用
【发布时间】:2014-04-25 12:30:55
【问题描述】:

我知道很多人问过这个问题,但我感到很无助,因为我尝试了我所知道的一切但仍然没有工作。

我提供了我最后一次尝试过的代码,该代码曾经有效,但后来就无效了。

public class ProximityAlertActivity extends Activity {

private static final long POINT_RADIUS = 100; // in Meters
private static final long PROX_ALERT_EXPIRATION = -1; // It will never expire
private static final String PROX_ALERT_INTENT = "com.androidmyway.demo.ProximityAlert";
private LocationManager locationManager;
private EditText latitudeEditText;
private EditText longitudeEditText;
private Button addAlertButton;

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

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        latitudeEditText = (EditText) findViewById(R.id.point_latitude);
        longitudeEditText = (EditText) findViewById(R.id.point_longitude);
        addAlertButton = (Button) findViewById(R.id.add_alert_button);

        addAlertButton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                     addProximityAlert();
              }
        });

}

private void addProximityAlert() {

       double latitude = Double.parseDouble(latitudeEditText.getText().toString());
       double longitude = Double.parseDouble(longitudeEditText.getText().toString());
       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
       );       
}

/****** BroadcastReceiver 显示通知 ******/

public class ProximityIntentReceiver extends BroadcastReceiver {
   private static final int NOTIFICATION_ID = 1000;

   @SuppressWarnings("deprecation")
   @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);

          Intent notificationIntent = new Intent(context, ProximityAlertActivity.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
          Notification notification = createNotification();
          notification.setLatestEventInfo(context, "Proximity Alert!", "You are near your point of interest."+key, 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_ALL;
          notification.defaults |= Notification.DEFAULT_LIGHTS;
          notification.ledARGB = Color.WHITE;
          notification.ledOnMS = 1500;
          notification.ledOffMS = 1500;
          return notification;
    }

}

/****** 清单文件 ******/

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidmyway.demo.proxymityalert"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

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


<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <activity
        android:name=".ProximityAlertActivity"
    >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver android:name="com.androidmyway.demo.proxymityalert.ProximityIntentReceiver"
              android:exported="false"
              android:enabled="true" >
        <intent-filter>
            <action android:name="com.androidmyway.demo.ProximityAlert" />
        </intent-filter>

    </receiver> 

</application>

请给我一些可行的解决方案。谢谢。

【问题讨论】:

    标签: android location proximity


    【解决方案1】:

    PROX_ALERT_INTENT = "com.androidmyway.demo.ProximityAlert";

    此字符串必须是触发接近警报时要触发的活动的名称。但是在我们的清单文件中,我看不到任何具有给定名称的活动。 你能纠正这个字符串的值吗?

    【讨论】:

    【解决方案2】:

    代码看起来不错,不知道你是怎么测试的,如果你使用任何 FAKE GPS 进行测试,它将无法正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多