안녕하세요 : 동일한 패키지의 서비스에 바인딩하는 활동을 가져올 수 없습니다.Android - 서비스 바인딩
활동은 다음과 같습니다
public class myApp extends TabActivity {
static private String TAG = "myApp";
private myService mService = null;
private ServiceConnection mServiceConn = new ServiceConnection(){
public void onServiceConnected(ComponentName name, IBinder service) {
Log.v(TAG, "Service: " + name + " connected");
mService = ((myService.myBinder)service).getService();
}
public void onServiceDisconnected(ComponentName name) {
Log.v(TAG, "Service: " + name + " disconnected");
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doBind();
Log.i(TAG, "Started (UI Thread)");
// set content
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
... add some tabs here....
tabHost.setCurrentTab(0);
}
private void doBind(){
Intent i = new Intent(this,myService.class);
if(bindService(i, mServiceConn, 0)){
Log.i(TAG, "Service bound");
} else {
Log.e(TAG, "Service not bound");
}
}
}
그런 다음 서비스 :
public class myService extends Service {
private String TAG = "myService";
private boolean mRunning = false;
@Override
public int onStartCommand(Intent intent, int flags, int startid) {
Log.i(TAG,"Service start");
mRunning = true;
Log.d(TAG,"Finished onStartCommand");
return START_STICKY;
}
/*
* Called on service stop
*/
@Override
public void onDestroy(){
Log.i(TAG,"onDestroy");
mRunning = false;
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
boolean isRunning() {
return mRunning;
}
/*
* class for binding
*/
private final IBinder mBinder = new myBinder();
public class myBinder extends Binder {
myService getService() {
return myService.this;
}
}
}
bindService 사실 반환하지만 onServiceConnection가 호출되지 않습니다 (mService는 항상 null, 그래서 내가 뭔가를 할 수 없어 같은 mService.isRunning())
서비스에 대한 매니페스트 항목은 그냥 :
<service android:name=".myService"></service>
안드로이드 개발자 사이트에서 직접 코드를 복사하고 있었지만 뭔가 놓쳤을 것입니다.
도움이 될 경우 답변을 수락하십시오. –