2014-09-21 108 views
3

我瘋了沒有得到我做錯了什麼,我現在轉向StackOverflow尋求幫助。Android Proximity警報 - 不觸發

我想添加接近警報位置,當接近警報觸發我收到通知。我在移動時獲得位置更新,並且我持續地將應用程序的距離打印到我的接近警報居中位置。

問題是proxmity警報從不觸發。嘗試此操作時,我一直在使用帶有實際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(); 

然後我有廣播接收機的代碼。這段代碼永遠不會運行,所以我從來沒有得到敬酒或通知。

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

你有沒有在清單中的正確位置的權限?另外,您能否成功地調用其他LocationManager方法(如requestLocationUpdates())? – stackoverflowuser2010 2014-09-25 06:33:08

+0

請參閱我的編輯以獲取我使用的權限。我用敬酒添加了requestLocationUpdates(),並且按預期顯示。所以LocationManager實例工作。 – Flipbed 2014-09-26 08:14:44

回答

3

你已經註冊在清單中的接收器?

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

Notify users when they reach a location

+0

不,從我理解我不必如果我使用context.registerReceiver方法。但我會盡快嘗試並回復你。 – Flipbed 2014-09-26 14:01:07

+0

我認爲,通過'Context#registerReceiver',接收者會在活動線程中被調用,所以它只會在活動啓動並運行時才起作用 – isalgueiro 2014-09-29 10:13:12

+0

@isalgueiro,您是對的!只要活動結束,接收方就會被取消註冊。翻轉,你有沒有嘗試過無線上網(你不需要有互聯網)? – Dyna 2014-10-01 16:14:15