2009-05-25 44 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);