2012-05-05 27 views
1

我目前正在研究一個內置感興趣點的地圖應用程序。 這些點應該通過接近警報觸發器通知用戶。 下面是我使用addProximityAlert和BroadcastReceivers

loc.addProximityAlert(lat, longe, radius, -1, PendingIntent.getActivity(
      c, 0, new Intent().putExtra(loc_name, loc_name), flag)); 

的想法是,一旦警報觸發警報對話框彈出有關使用該選項的網站很短的書籍說明要麼關閉警告或得到addproximityAlert()代碼更多信息(使用WebView)。

到目前爲止,我沒有運行時或編譯時的錯誤,但當我接近每個站點時,什麼也沒有發生。

我關於爲什麼沒有發生的理論是:

1)I沒有正確使用的PendingIntent,或

2)I還沒有設置的廣播接收器正確地

下面是BroadcastRecevier的XML代碼,

<receiver android:name=".ProxyAlertReceiver" > 
     <intent-filter> 
      <action android:name="entering" /> 
     </intent-filter> 
    </receiver> 

我目前的解決這個問題的方案是修改PendingIntent來使用這樣一個新的Intent;

...new Intent(myContext, ProxyAlertReceiver.class)... 

看看我是否得到任何結果。

對我的問題的意見和建議將不勝感激!

+0

您是否已通過創建一個簡單的測試活動來測試您的BroadcastReceiver Activity,該活動廣播一個Intent以查看您的意圖過濾器是否正確安裝? –

+0

是的,儘管我嘗試了幾種不同的廣播。我已經試過TIME_TICK,sendBroadcast在一個定製的Intent上指向BroadcastReceiver(儘管我不知道如何完全寫出xml intent-filter),並且我也嘗試了一個PendingIntent,但也失敗了。沒有崩潰,但沒有任何反應。 – user1319430

回答

2

您是否試過PendingIntent.getBroadcast(...)?

Intent locationReachedIntent = new Intent("entering"); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1234, 
    locationReachedIntent, 0); 

locationManager.addProximityAlert(longitude, latitude, radius, -1, pendingIntent); 

我有上面的代碼在我的應用程序工作。

1

使用此

Intent locationIntent = new Intent(); 
Bundle extras= new Bundle(); 
extras.putString("loc_name",loc_name); 
locationIntent.putExtras(extras); 
PendingIntent pendingIntent= new PendingIntent.getActivity(this,0,locationIntent,0); 
loc.addProximityAlert(lat, longe, radius, -1, pendingIntent, flag)); 

我假設你loc_name是一個字符串。這將工作。

0

實現接近警報取決於不僅僅調用Location Manager上的addProximity方法。

您還必須:

  1. 創建一個接收器類時觸發警報時會觸發,並會收到一個狀態(進入或退出)和action name *;

    public class ProximityReceiver extends BroadcastReceiver { 
    public String TAG ="ProxReceiver"; 
    
    @Override 
    public void onReceive(Context context, Intent intent) { 
        /* your code here - sample below */ 
        final String key = LocationManager.KEY_PROXIMITY_ENTERING; 
        final Boolean entering = intent.getBooleanExtra(key, false); 
        if (entering) { 
         Toast.makeText(context, "LocationReminderReceiver entering", Toast.LENGTH_SHORT).show(); 
         Log.v(TAG, "Poi entering"); 
        } else { 
         Toast.makeText(context, "LocationReminderReceiver exiting", Toast.LENGTH_SHORT).show(); 
         Log.v(TAG, "Poi exiting"); 
        } 
        Log.v(TAG,"Poi receive intent["+intent.toString()+"]"); 
    
        Bundle extras = intent.getExtras(); 
        // debugging only 
        int counterExtras = extras.size(); 
        if (extras != null) { 
         for (String key : extras.keySet()) { 
         Object value = extras.get(key); 
         Log.d(TAG, "Prox Poi extra "+String.format("key[%s] value[%s] class[%s] count[%s]", key, 
           value.toString(), value.getClass().getName(), String.valueOf(counterExtras))); 
         } 
        } else { 
         Log.v(TAG, "Prox Poi extra empty"); 
        } 
    } 
    } 
    
  2. 在你的Manifest文件中聲明這個接收器;

    <receiver android:name=".ProximityReceiver" > 
        <intent-filter> 
         <action android:name="my" /> 
        </intent-filter> 
    </receiver> 
    
  3. 註冊(關聯尚待處理的意圖來)這個接收器,加上接近警報(S)。只在你的代碼中註冊你的接收器。如果多次註冊一個接收者,它會針對每個接收者實例觸發一次(您到達一個POI,它將註冊一個名爲"my"的未決意圖。**

    // create proximity alert 
    Intent locationIntent = new Intent("my"); 
    ProximityReceiver proximityReceiver = new ProximityReceiver(); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(mapView.getContext(), <some identifying text>, 
         locationIntent, 0); 
    loc.addProximityAlert(lat, longe, radius, -1, PendingIntent.getActivity(
        c, 0, new Intent().putExtra(loc_name, loc_name), flag)); 
    
    IntentFilter filter = new IntentFilter("my"); 
    context.registerReceiver(proximityReceiver, filter); 
    

哪裏context可以this如果在同一活動上運行。

除非你想繼續接收警報,即使在後臺(甚至終止),則必須實現在和onResume方法去除和接近警報的再創造,像this SO question(跳到問題結束)。

注*在這個例子中,"my"將是動作名稱(參見Intent聲明)的一個動作,將與意圖,並且含有至少,關鍵enteringLocationManager.KEY_PROXIMITY_ENTERING)與布爾額外的包一起傳遞值,如果正在輸入(1)或退出(0)接近半徑,則會爲您提供警報的狀態。

note **如果您多次註冊了"my"的接收器,它將針對每個接近報警事件觸發多次,該事件調用名爲"my"的意圖。

相關問題