내 서비스를 사용하려고 할 때 프로그램이 실패하는 문제가 있습니다. 나는이가 그런서비스에 연결되었지만 사용하려고 할 때 실패 함
startService(new Intent(this, GPSService.class));
(메인라는) 다른 활동 : 처음 활동을 시작한다
다음 나는 주요, doBindService을 (전화 상속protected GPSService gService;
protected boolean mIsBound = false;
protected ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
GPSServiceBinder binder = (GPSServiceBinder) service;
gService = binder.getServerInstance();
mIsBound = true;
Toast.makeText(ActionBarActivity.this, "Attached to a process",
Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName className) {
gService = null;
Toast.makeText(ActionBarActivity.this, "Deattachhed from process",
Toast.LENGTH_SHORT).show();
}
};
protected void doBindService(Activity act) {
getApplication().bindService(new Intent(act,
GPSService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
if(mIsBound)
Toast.makeText(ActionBarActivity.this, "connected",
Toast.LENGTH_SHORT).show();
};
)하고, gService를 사용하려고 NullPointer 예외가 발생하면 실패합니다.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doBindService(OccupationActivity.this);
gService.getLocation();
그러나 onServiceConnected()의 토스트가 제대로 작동합니다. 당신이 doBindService()
를 호출 할 때이 서비스에 바인딩을 시도하기 때문
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doBindService(OccupationActivity.this);
gService.getLocation();
하지만,이 문제가 해결되지 수
public class GPSService extends Service {
private static final String TAG = "GPS_SERVICE";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
Location currentLocation;
private class LocationListener implements android.location.LocationListener{
Location mLastLocation;
public LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location)
{
Log.e(TAG, "onLocationChanged: " + location);
if (location != null){
mLastLocation.set(location);
currentLocation = mLastLocation;
}
}
@Override
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
private IBinder mBinder = new GPSServiceBinder();
public class GPSServiceBinder extends Binder {
public GPSService getServerInstance() {
return GPSService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
int minTime = 60000;
float minDistance = 15;
@Override
public void onCreate()
{
super.onCreate();
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
public Location getLocation()
{
return currentLocation;
}
public boolean getGPSstatus()
{
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
return true;
}else{
return false;
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}
나는 메서드를 만들었고 내 액티비티의 button_click에서이 메서드를 호출했다. 하지만 내 응용 프로그램은 nullPointer whern 임 gService를 사용하려고 아직도 충돌입니다. – v1pka
** onServiceConnected()가 호출 된 후에 ** gService를 사용할 수 없습니다. 그렇지 않으면'gService'가 여전히 null입니다. 디버거로 단계별 실행. 'onServiceConnected()'에 중단 점을 설정하십시오. 전화가 걸려 있는지 확인하십시오. 그런 다음 사용하려고하는 중단 점을 설정하십시오. 그래도 문제가 발생하면 코드를 게시하십시오. –
고맙습니다, 데이빗. 나는 오류를 발견했다. 문제는 onServiceConnected가 아니었다. 서비스가 기본 위치 (0.0, 0.0) 대신에 null을 반환했다는 것이 문제였다. – v1pka