2013-05-10 80 views
0

在我的項目中,我使用活頁夾機制與遠程服務進行通信。遠程服務類將每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 .` 

回答

1

您錯過了任何方式讓服務通知活動有關新事件。從活動到服務的呼叫intFromJNI是一次性的函數調用,只發生一次 - 它沒有任何責任告訴用戶界面未來的變化。

您應該添加一些偵聽器類。例如,添加一些IMyServiceListener.aidl這樣的:你IMyService.Stub類中

import com.something.IMyServiceListener; 

void addListener(in IMyServiceListener newListener); 

然後,你將需要實現addListener - 你應該添加結果對象:

package com.something; 

interface IMyServiceListener { 
    void valueUpdated(int newValue); 
} 

然後在IMyService.aidl添加此到某些監聽器陣列,並在每個監聽器上調用valueUpdated,每次值改變。

中的活動,你應該叫myService.addListener,並通過在接收的變化通知一些對象。然後,您可以在活動內的用戶界面上顯示該內容。