2014-10-11 4 views
0

바운드 서비스의 자습서를 따르고 있습니다 : http://developer.android.com/guide/components/bound-services.html 그리고 내 코드에서 사용하고 있습니다. 하지만 자습서의 코드를 사용하면 다음 줄에 오류가 표시됩니다. service = binder.getService(); localService에서 IBinder로 변환 할 수 없습니다. 그래서 솔루션으로 IBinder로 캐스팅했습니다. 하지만 classCastException이 발생하면 앱을 실행하기 시작합니다. 바인딩 서비스를 사용할 때 ClassCastException

는 MainActivity에서 코드입니다 :

/** Defines callbacks for service binding, passed to bindService() */ 
private ServiceConnection mConnection = new ServiceConnection() { 

    @Override 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     // We've bound to LocalService, cast the IBinder and get LocalService instance 
     LocalBinder binder = (LocalBinder) service; 
     service = (IBinder) binder.getService(); 
     mBound = true; 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName arg0) { 
     mBound = false; 
    } 
}; 


@Override 
protected void onStart() { 
    super.onStart(); 
    // Bind to LocalService 
    Intent intent = new Intent(this, LocalService.class); 
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    // Unbind from the service 
    if (mBound) { 
     unbindService(mConnection); 
     mBound = false; 
    } 
} 

내 서비스의 코드는 다음과 같습니다

public class LocalService extends Service implements LocationListener, 
GooglePlayServicesClient.ConnectionCallbacks, 
GooglePlayServicesClient.OnConnectionFailedListener, 
com.google.android.gms.location.LocationListener, SensorEventListener { 


/** 
    * Class used for the client Binder. Because we know this service always 
    * runs in the same process as its clients, we don't need to deal with IPC. 
    */ 
    public class LocalBinder extends Binder { 
     LocalService getService() { 
      // Return this instance of LocalService so clients can call public methods 
      return LocalService.this; 
     } 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return mBinder; 
    } 
} 

을 그리고 내 매니페스트 파일은 다음과 같습니다

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.geotodo" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

<!-- Required for accessing our current location --> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="19" /> 

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true"/> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <meta-data 
    android:name="com.google.android.maps.v2.API_KEY" 
    android:value="KEY"/> 

    <meta-data android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version" /> 

    <service android:enabled="true" android:name=".LocalService" /> 
</application> 

누군가 도와주세요.

+0

왜 서비스를 설정 하시겠습니까? ...? 그것의 메서드 매개 변수 ... – pskink

+0

첫째로 당신은 클라이언트와 서비스 구현에 동일한 라이브러리를 사용하고 둘째로 동일한 루트 패키징 이름 인 my/project/service/내에서 서비스를 확장하고 있는지, impl 그것은 서비스 impl 루트 디렉토리와 클라이언트에서 동일해야하며 서비스를 확인하십시오 var – AntJavaDev

답변

1

사용 :

service = (LocalService) binder.getService(); 

... 대신 :

service = (IBinder) binder.getService(); 

당신은 Service 객체를 호출하고 그것이 ClassCastException을주고있다 IBinder 객체에 캐스팅 입력하려고합니다.

+0

저를 위해 일한 Ok :) – user3801539

관련 문제