在我的項目中,我使用活頁夾機制與遠程服務進行通信。遠程服務類將每10秒調用一次JNI函數並更新到UI。 JNI函數具有下面的代碼Binder mechanisam更新Android中的UI
static int n = 100;
return ++n;
這JNI函數調用服務類和更新像下面
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
}
};
這裏我的問題是,該服務類沒有爲每10秒更新用戶界面UI和該值不會在UI中增加,但該值在服務類中每10秒遞增一次,即以下打印語句每隔10秒更新一次,該值在上面代碼(Service)的第7行中。
System.out.println("inside ****** stub"+libData);
但我想在UI中更新它。即語句行第10行。
System.out.println("value if JNI==>"+jniValue);
I`t is not happening in my code . How to solve this one .`