멋진 앱을 만들고 있습니다. 나는 더 많은 것을 알지 못합니다.하지만 서비스에서 액티비티로 객체를 전달하는 방법을 알고 있습니다. 귀하는 귀하의 서비스에 AIDL을 할 수 있습니다. AIDL이 당신의 서비스 대상을 활동에 전달합니다. 그런 다음 활동 UI를 업데이트 할 수 있습니다. 이 link이 도움이 될 수 있습니다!
먼저 편집기로 .aidl 파일을 만들어이 파일을 바탕 화면에 저장해야합니다. AIDL은 다른 어떤 인터페이스와도 같습니다. 마찬가지로, ObjectFromService2Activity.aidl
package com.yourproject.something
// Declare the interface.
interface ObjectFromService2Activity {
// specify your methods
// which return type is object [whatever you want JSONObject]
JSONObject getObjectFromService();
}
지금이 파일을 복사 및 붙여 넣기 프로젝트 폴더와 ADT 플러그인은 자동으로 세대/폴더에 ObjectFromService2Activity 인터페이스와 스텁을 생성합니다.
Android SDK에는 Eclipse를 사용하지 않을 경우를 대비하여 Java 코드를 생성하는 데 사용할 수있는 (명령 줄) 컴파일러 aidl (tools/디렉토리에 있음)이 포함되어 있습니다.
서비스에서 obBind() 메소드를 덮어 씁니다. 마찬가지로, Service1.java는
public class Service1 extends Service {
private JSONObject jsonObject;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate()");
jsonObject = new JSONObject();
}
@Override
public IBinder onBind(Intent intent) {
return new ObjectFromService2Activity.Stub() {
/**
* Implementation of the getObjectFromService() method
*/
public JSONObject getObjectFromService(){
//return your_object;
return jsonObject;
}
};
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy()");
}
}
는 활동을 사용하여 서비스를 시작
또는이 서비스를 시작하고 ServiceConnection을 할 위치. 마찬가지로,
Service1 s1;
private ServiceConnection mConnection = new ServiceConnection() {
// Called when the connection with the service is established
public void onServiceConnected(ComponentName className, IBinder service) {
// Following the example above for an AIDL interface,
// this gets an instance of the IRemoteInterface, which we can use to call on the service
s1 = ObjectFromService2Activity.Stub.asInterface(service);
}
// Called when the connection with the service disconnects unexpectedly
public void onServiceDisconnected(ComponentName className) {
Log.e(TAG, "Service has unexpectedly disconnected");
s1 = null;
}
};
ObjectFromService2Activity의 객체를 사용하면 메소드 s1.getObjectFromService()가 JSONObject를 반환 할 수 있습니다. More Help 재미 있습니다!
http://stackoverflow.com/questions/3747448/android-passing-data-between-service-and-activity 또는 http://stackoverflow.com/questions/9239240/passing -object-through-intent-from-background-service-to-an-activity –