2013-05-10 3 views
0

내 프로젝트에서 바인더 메커니즘을 사용하여 원격 서비스와 통신합니다. 원격 서비스 클래스는 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 .` 

답변

1

당신은 서비스가 새로운 이벤트에 대한 활동을 이야기 할 수있는 방법 누락을 즉. 액티비티에서 서비스로의 호출, intFromJNI은 한 번만 발생하는 일회성 함수 호출이며 향후 변경 사항을 UI에 알리는 데 지속적인 책임이 없습니다.

일부 수신기 클래스를 추가해야합니다. 예를 들어, 다음과 같이 뭔가 IMyServiceListener.aidl를 추가 : 다음

package com.something; 

interface IMyServiceListener { 
    void valueUpdated(int newValue); 
} 

IMyService.aidl이 추가 : 다음

import com.something.IMyServiceListener; 

void addListener(in IMyServiceListener newListener); 

를, 당신의 IMyService.Stub 클래스 내에서 당신이 addListener를 구현해야합니다 - 당신은 결과 객체를 추가해야 일부 청취자 배열에 적용하고 값이 변경 될 때마다 각 청취자에 valueUpdated을 호출하십시오.

활동 내에서 myService.addListener을 호출하고 변경 사항에 대한 알림을 수신하는 개체를 전달해야합니다. 그런 다음 활동 내 UI에이를 표시 할 수 있습니다.

관련 문제