Mau*_*bon 4 gps android android-sensors
当手机靠近某个位置时,我正试图更新并更新.我都尝试过使用addProximityAlert和requestLocationUpdates
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);
Run Code Online (Sandbox Code Playgroud)
这个从不激发意图(我知道它可以工作,因为我可以手动启动它).我尝试使用一个监听器但它只执行一次.无论我多少次更新gps,它都不会被再次调用
ProximityListener pl = new ProximityListener();
lm. requestLocationUpdates("gps", 2000, 10, pl);
Run Code Online (Sandbox Code Playgroud)
这是活动的代码(在第一种情况下调用)
public class ProximityAlert extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("intent", "Hi");
finish();
}
Run Code Online (Sandbox Code Playgroud)
这是听众
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
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我认为问题在于你如何定义Intent/PendingIntent.使用Intent启动Activity有两种方法,你所包含的代码看起来像是两者之间的交叉.
启动Activity的标准方法是使用Intent构造函数,该构造函数接受当前上下文和Activity的类,并使用getActivity方法on PendingIntent来创建PendingIntent:
Intent intent = new Intent(this, ProximityAlert.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Run Code Online (Sandbox Code Playgroud)
或者,您可以IntentReceiver使用侦听特定操作的IntentFilter(例如"eu.mauriziopz.gps.ProximityAlert")向Manifest中的Activity 添加一个.但是,在这种情况下,您需要使用PendingIntent.getBroadcast创建PendingIntent.
Intent intent = new Intent("eu.mauriziopz.gps.ProximityAlert");
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Run Code Online (Sandbox Code Playgroud)
在所有情况下,您都需要确保您已获得清单中定义的基于位置的服务的正确权限:
<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"/>
Run Code Online (Sandbox Code Playgroud)
另外,您可以考虑使用静态常量,而不是使用字符串"gps" LocationManager.GPS_PROVIDER.
| 归档时间: |
|
| 查看次数: |
4485 次 |
| 最近记录: |