2015-04-15 68 views
1

我知道如何通過aidl接口從活動中調用服務方法。但是,如何從運行在獨立進程中的服務中調用方法,而不需要廣播接收器?如何在獨立過程中從服務中調用活動中的方法?

有沒有什麼辦法可以通過相同的aidl接口或其他java接口在我的活動中調用方法?

代碼:

//aidl interface 
interface IRemoteServiceCallback { 

    void valueChanged(); 
} 

//starting service in activity 
Intent serviceIntent = new Intent(BackgroundService.class.getName()); 
serviceIntent.setPackage("com.example.service2"); 
startService(serviceIntent); 
bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE); 


//aidl stub implementation in activity 
private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() { 

    @Override 
    public void valueChanged() { 

     System.out.println("Callback method called"); 
    } 
}; 

//service connection in activity 
BackgroundService mService = null; 
private ServiceConnection mConnection = new ServiceConnection() { 

    public void onServiceConnected(ComponentName className, IBinder service) { 

     System.out.println("Callback service connected"); 
     try { 

      mService.registerCallback(mCallback); 
     } catch (Exception e) { 

      Log.e("Service2-CallbackService-Connecting:", e.toString()); 
     } 
    } 

    public void onServiceDisconnected(ComponentName className) { 

     if (mService != null) { 
      try { 
       mService.unregisterCallback(mCallback); 
      } catch (Exception e) { 
       Log.e("Service2-CallbackService:", e.toString()); 
      } 
     } 
    } 
}; 

// registering callbacks in service 
public void registerCallback(IRemoteServiceCallback mCallback) { 

    System.out.println("Callback registers..."); 
    this.mCallback = mCallback; 
} 

public void unregisterCallback(IRemoteServiceCallback mCallback2) { 

    this.mCallback = null; 
} 

//calling method 
mCallback.valueChanged(); 

回答

1

http://developer.android.com/guide/components/aidl.html

您可以通過通過服務連接註冊接口使用回調。

/** 
    * This implementation is used to receive callbacks from the remote 
    * service. 
    */ 
    private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() { 
     /** 
     * This is called by the remote service regularly to tell us about 
     * new values. Note that IPC calls are dispatched through a thread 
     * pool running in each process, so the code executing here will 
     * NOT be running in our main thread like most other things -- so, 
     * to update the UI, we need to use a Handler to hop over there. 
     */ 
     public void valueChanged(int value) { 
      mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value, 0)); 
     } 
    }; 

    private static final int BUMP_MSG = 1; 

    private Handler mHandler = new Handler() { 
     @Override public void handleMessage(Message msg) { 
      switch (msg.what) { 
       case BUMP_MSG: 
        mCallbackText.setText("Received from service: " + msg.arg1); 
        break; 
       default: 
        super.handleMessage(msg); 
      } 
     } 

    }; 

//從活動註冊回調

private ServiceConnection mConnection = new ServiceConnection() { 
      public void onServiceConnected(ComponentName className, 
        IBinder service) { 
       // This is called when the connection with the service has been 
       // established, giving us the service object we can use to 
       // interact with the service. We are communicating with our 
       // service through an IDL interface, so get a client-side 
       // representation of that from the raw service object. 
       .... 

       // We want to monitor the service for as long as we are 
       // connected to it. 
       try { 
        mService.registerCallback(mCallback); 
       } catch (RemoteException e) { 
        // In this case the service has crashed before we could even 
        // do anything with it; we can count on soon being 
        // disconnected (and then reconnected if it can be restarted) 
        // so there is no need to do anything here. 
       } 
      } 

      public void onServiceDisconnected(ComponentName className) { 
       // This is called when the connection with the service has been 
       // unexpectedly disconnected -- that is, its process crashed. 
       ... 
       if (mService != null) { 
        try { 
         mService.unregisterCallback(mCallback); 
        } catch (RemoteException e) { 
         // There is nothing special we need to do if the service 
         // has crashed. 
        } 
       } 
      } 
     }; 

需要 大段引用

當您需要的服務, 創造registerCallback和unregisterCallback和調用接口

//服務的代碼片段

IRemoteServiceCallback mCallback; 
    public void registerCallback(IRemoteServiceCallback callback) { 
     this.mCallback = callback; 
    } 

    public void unregisterCallback() { 
     this.mCallback = null; 
    } 
    . 
    . 
    private void updateActivity() { 
     if(mCallback != null) { 
      **mCallback.valueChanged(10);** 
     } 
    } 
+0

他們說的關於活動中的回調存根實現。但他們沒有說任何關於從服務中調用這個存根方法的事情。 – Sushant

+0

檢查我更新的答案。 – suresh

+0

是的。這是活動部分。服務部分怎麼樣?如何從服務中調用此服務? – Sushant

相關問題