내 프로젝트에서 바인더 메커니즘을 사용하여 원격 서비스와 통신합니다. 원격 서비스 클래스는 JNI 함수를 호출하고 10 초마다 UI로 업데이트합니다. JNI 함수 코드 아래에있다안드로이드에서 UI를 업데이트하는 바인더 메커니즘
static int n = 100;
return ++n;
서비스 클래스에서 호출하고 내 문제는, 서비스 클래스마다 10 초 동안 UI를 업데이트하지 않습니다 여기
public class MyService extends Service{
private static final String TAG = "MyService";
private final Handler serviceHandler = new Handler();
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("inside service onCreate");
Log.d(TAG, "entered onStart");
serviceHandler.removeCallbacks(sendUpdatesToUI);
serviceHandler.postDelayed(sendUpdatesToUI, 1000); // 1 second
}
1. private IMyService.Stub bioStub = new IMyService.Stub() {
2.
3. @Override
4. public int intFromJNI() throws RemoteException {
5. // TODO Auto-generated method stub
6. int libData = Abcd.intFromJNI();
7. System.out.println("inside ****** stub"+libData);
8. return libData;
}
};
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("inside binder ");
return bioStub;
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
Log.d(TAG, "entered Runnable");
try {
bioStub.intFromJNI();
serviceHandler.postDelayed(this, 10000);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
This service class is calling in Activty
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Intent serviceIntent=new Intent();
serviceIntent.setClass(context, MyService.class);
boolean ok=bindService(serviceIntent, mServiceConnection , Context.BIND_AUTO_CREATE);
Log.v("ok", String.valueOf(ok));
}
private ServiceConnection mServiceConnection=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName arg0, IBinder service) {
// TODO Auto-generated method stub
System.out.println("inside ServiceConnection");
myService = IMyService.Stub.asInterface(service);
try {
Log.d("Client", "entered mServiceConnection");
8. int jniValue = myService.intFromJNI();
9. System.out.println("value if JNI==>"+jniValue);
10.
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};
아래처럼 UI 업데이트하기이 JNI 기능과 값은 UI에서 증가하지 않지만 서비스 클래스에서 매 10 초마다 값이 증가합니다. 즉, 아래의 print 문은 위 코드 (서비스)의 행 번호 7에있는 매 10 초마다 업데이트됩니다. 그것은 또한 UI에서 업데이트에
System.out.println("inside ****** stub"+libData);
는하지만합니다. 문 라인 NUM (10)
System.out.println("value if JNI==>"+jniValue);
I`t is not happening in my code . How to solve this one .`