【问题标题】:Android Proximity Alert - Not TriggeringAndroid 接近警报 - 未触发
【发布时间】:2014-09-21 08:18:00
【问题描述】:

我不知道自己做错了什么快疯了,现在我正在向 StackOverflow 寻求帮助。

我正在尝试使用接近警报添加位置,当接近警报触发时,我会收到通知。我在四处走动时会收到位置更新,并且我有应用程序不断打印我到接近警报中心位置的距离。

问题是接近警报永远不会触发。尝试此操作时,我一直在使用带有实际 GPS 的物理设备。

设置接近警报的代码。显示了最后的吐司,所以我知道这段代码运行了。

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

    Intent intent = new Intent(PROX_ALERT_INTENT);
    intent.putExtra(PROXIMITY_ITEM_ID, item.getID());
    PendingIntent proximityIntent = PendingIntent.getBroadcast(this, item.getID(), intent, 0);

    locationManager.addProximityAlert(
        item.getPlace().getLocation().latitude, // the latitude of the central point of the alert region
        item.getPlace().getLocation().longitude, // the longitude of the central point of the alert region
        200, // the radius of the central point of the alert region, in meters
        -1, // 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);
    this.registerReceiver(new ProximityAlertBroadcastReceiver(), filter);
    Toast.makeText(this, "Added new proximity alert event...", Toast.LENGTH_SHORT).show();

然后我有了广播接收器的代码。这段代码永远不会运行,所以我永远不会收到 toast 或通知。

public class ProximityAlertBroadcastReceiver extends BroadcastReceiver {

    private void createNotification(Context context, ToDoItem todoItem) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.todomaps_icon)
                .setContentTitle(todoItem.getTask())
                .setContentText("You are " + todoItem.getDistanceString() + " to todo: " + todoItem.getTask());

        // Sets an ID for the notification
        int mNotificationId = 001;
        // Gets an instance of the NotificationManager service
        NotificationManager mNotifyMgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        // Builds the notification and issues it.
        mNotifyMgr.notify(mNotificationId, mBuilder.build());
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "ON RECEIVE!", Toast.LENGTH_SHORT).show(); //TODO: TEMP
        int itemID = intent.getIntExtra(AddItemActivity.PROXIMITY_ITEM_ID, -1);
        DBHandler handler = new DBHandler(context);
        Item item = handler.getItem(itemID);
        createNotification(context, item);
    }

}

我不知道这是否有所不同,但应用程序中用于查看到接近警报中心距离的位置管理器实例是用于创建接近警报的不同实例。所以创建接近警报的位置管理器实例没有请求位置更新,但我猜这并不重要。

这是我使用的权限:

<permission 
    android:name="com.flipsoft.todomaps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"></permission>

<uses-permission android:name="com.flipsoft.todomaps.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-feature
  android:glEsVersion="0x00020000"
  android:required="true"/>

【问题讨论】:

  • 您在清单中是否拥有正确的位置权限?另外,你能成功调用其他LocationManager方法,比如requestLocationUpdates(),成功吗?
  • 查看我的编辑以了解我使用的权限。我添加了 requestLocationUpdates() 和祝酒词,它按预期显示。所以 LocationManager 实例可以工作。

标签: java android android-notifications


【解决方案1】:

您是否在 Manifest 中注册了接收方?

<receiver android:name="com.example.ProximityAlertBroadcastReceiver" >
    <intent-filter>
        <action android:name="<your intent>" />
    </intent-filter>
</receiver>

Notify users when they reach a location

【讨论】:

  • 不,据我了解,如果我使用 context.registerReceiver 方法,我不必这样做。但我会尽快尝试并回复您。
  • 我认为使用Context#registerReceiver 接收器会在活动线程中被调用,因此只有在该活动启动并运行时它才会起作用
  • @isalgueiro,你是对的!一旦活动结束,接收者就被取消注册。 Flipbed,你有没有尝试过打开wifi(你不需要上网)?
  • 现在开始工作了!显然,从添加事件到触发它需要一些时间(约 1 分钟)。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多