我正在尝试创建一个更新当前gps位置的后台服务。我在第NullPointerException
行<{1}}
HomeActivity启动服务
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Service(ForHire)创建一个TimerTask更新
startService(new Intent(getApplicationContext(), ForHire.class));
第一个问题是我得到了NullPointerException。
第二个问题是,如果我将这两行注释掉public class ForHire extends Service {
...
private Timer getUpdatesNow = new Timer();
private Updates updates = new Updates(getUpdatesNow);
@Override
public void onCreate() {
...
getUpdatesNow.schedule(updates, God.KM20TIME);
Log.v("Taxeeta", "Notification created");
}
private class Updates extends TimerTask implements LocationListener {
private Timer getUpdatesNow;
public Updates(Timer newGetUpdatesNow) {
super();
getUpdatesNow = newGetUpdatesNow;
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
God.KM20TIME, God.KM20DISTANCE, (LocationListener) this);
}
public void run() {
...
//do some cleanup
}
@Override
public void onLocationChanged(Location location) {
Log.v("Taxeeta", "Location changed");
// do a update of the current location.
}
,则永远不会调用onLocationChanged。
我的清单
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, God.KM20TIME, God.KM20DISTANCE, (LocationListener) this);
我在这里缺少什么?
编辑:<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" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application ...>
...
<uses-library android:name="com.google.android.maps" />
</application>
(5秒)和Values of KM20TIME = 5000
(1米)。除了下面的修理,我走出了我的房子,启用了GPS,从阳台的一个角落走到另一个角落。我注意到我的gps(LSB)在每隔5秒钟从一个角落走到另一个角落时发生了变化。
答案 0 :(得分:11)
您最早可以拨打new Updates(getUpdatesNow);
的电话是在您的服务的onCreate()
方法中。原因很简单,getSystemService()
需要一个在进入onCreate()
之前不存在的有效上下文。
尝试:
private Timer getUpdatesNow;
private Updates updates;
@Override
public void onCreate() {
...
getUpdatesNow = new Timer();
updates = new Updates(getUpdatesNow);
答案 1 :(得分:3)
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
Toast.makeText(getBaseContext(),
"Last Location is found..",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(),
"Last Location is not found..",
Toast.LENGTH_LONG).show();
}
LocationListener locationListener = new LocationListener() {
public void onStatusChanged(String provider,
int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(),
"Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(),
"Gps Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onLocationChanged(Location location) {
Log.i("geolocation",
"lat is : " + location.getLatitude()
+ " and " + location.getLongitude()
+ "");
Toast.makeText(getBaseContext(),
"Location is Changed..", Toast.LENGTH_LONG)
.show();
}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
答案 2 :(得分:2)
@Override
public void onCreate() {
new Updates(getUpdatesNow);
...
getUpdatesNow.schedule(updates, God.KM20TIME);
Log.v("Taxeeta", "Notification created");
}