2009-05-25 46 views
4

我試圖在手機靠近位置時獲取並更新。 我都嘗試使用addProximityAlert和requestLocationUpdatesaddProximityAlert不起作用(requestLocationUpdates也不起作用) - Android

LocationManager lm =(LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Location l = lm.getLastKnownLocation("gps"); 
    Intent intent = new Intent("eu.mauriziopz.gps.ProximityAlert"); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 
    lm.addProximityAlert(12, 12, 100, 1000000, pIntent); 

這一個永遠不會觸發的意圖(我知道的作品,因爲我可以手動啓動它)。 我試着用一個監聽器,但它只被執行一次。不管有多少次我更新它從來沒有被稱爲GPS再次

ProximityListener pl = new ProximityListener(); 
    lm. requestLocationUpdates("gps", 2000, 10, pl); 

這是活動(稱爲第一種情況)

public class ProximityAlert extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    Log.d("intent", "Hi"); 
    finish(); 
} 

這是代碼監聽

public class ProximityListener implements LocationListener { 
String DEBUG_TAG = "ProximityListener"; 
@Override 
public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 
    Log.d(DEBUG_TAG, location.toString()); 

} 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 

} 
} 

謝謝

回答

0

我不認爲你應該終止已在onCreate方法中的Activity od(通過調用finish())。

+0

這僅僅是一個調試的活動,我很感興趣,那麼它寫在日誌中,所以整理活動沒有按變化不大。 但謝謝你的答案 – 2009-06-03 19:20:08

5

我認爲問題在於你如何定義你的意圖/ PendingIntent。有兩種方法可以使用Intent啓動一個Activity,並且你所包含的代碼看起來像是兩者之間的交叉。

開始一個活動的標準方法是使用意向構造函數,當前上下文和活動的類並使用上PendingIntentgetActivity方法創建的PendingIntent:

Intent intent = new Intent(this, ProximityAlert.class); 
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 

或者,您可以添加一個IntentReceiver添加到您的清單中的活動,並帶有一個偵聽特定操作的IntentFilter(如「eu.mauriziopz.gps.ProximityAlert」)。但是,在這種情況下,您需要使用PendingIntent.getBroadcast來創建PendingIntent。

Intent intent = new Intent("eu.mauriziopz.gps.ProximityAlert"); 
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0); 

在你需要確保你已經得到了你的清單中定義的基於位置的服務的正確權限的所有情況:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.INTERNET"/> 

此外,而不是使用字符串「 gps「,你可以考慮使用靜態常量LocationManager.GPS_PROVIDER

1
Intent intent = new Intent("com.sccp.fourgdummy.REQUEST_DFGS"); 
intent.putExtra("launch_tab", "otp"); 
PendingIntent proximityIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); 

這是很重要的:Intent.FLAG_ACTIVITY_NEW_TASK

1

的API定義的addProximityAlert參數爲緯度,經度,半徑,到期,意圖。您是否嘗試過的到期時間設置爲-1而非100000

lm.addProximityAlert(12, 12, 100, 1000000, pIntent);

lm.addProximityAlert(12, 12, 100, -1, pIntent);