2016-01-20 31 views
0

我遵循Android開發人員手冊中描述的方法。 這裏是我的aidl接口使用aidl調用另一個應用中的服務方法

package com.test.aidl;

interface IMyAidl{ 

    int add(int n1, int n2); 
} 

我的貢獻莫過於類

package com.test.usingaidl; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.os.RemoteException; 
import android.support.annotation.Nullable; 

import com.test.aidl.IMyAidl; 

/** 
* Created by HarshVardhan on 1/18/2016. 
*/ 
public class AddService extends Service { 
    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return mBinder; 
    } 
    private final IMyAidl.Stub mBinder = new IMyAidl.Stub() { 
     @Override 
     public int add(int n1, int n2) throws RemoteException { 
      return n1+n2; 
     } 
    }; 


} 

我的MainActivity

package com.test.usingaidl; 

import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.os.IBinder; 
import android.os.RemoteException; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.test.aidl.IMyAidl; 

public class MainActivity extends AppCompatActivity { 

    IMyAidl service; 
    MServiceConnection con; 

    class MServiceConnection implements ServiceConnection{ 

     @Override 
     public void onServiceConnected(ComponentName name, IBinder bound) { 
      service =IMyAidl.Stub.asInterface(bound); 
      Toast.makeText(MainActivity.this,"Service Connected!",Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      service = null; 
      Toast.makeText(MainActivity.this,"Service Disconnected!",Toast.LENGTH_LONG).show(); 
     } 
    } 

    private void initService(){ 
     con = new MServiceConnection(); 
     Intent i = new Intent(); 
     i.setClassName(this, com.test.usingaidl.AddService.class.getName()); 
     if(!bindService(i, con, Context.BIND_AUTO_CREATE)){ 
      Toast.makeText(this, "Bind Service Failed!", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    private void releaseService(){ 
     unbindService(con); 
     con = null; 
    } 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     initService(); 
     Button b = (Button) findViewById(R.id.button); 
     final EditText et1 = (EditText) findViewById(R.id.editText); 
     final EditText et2 = (EditText) findViewById(R.id.editText2); 
     final TextView tv = (TextView) findViewById(R.id.textView); 
     b.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try { 
        int i = service.add(Integer.parseInt(et1.getText().toString()),Integer.parseInt(et2.getText().toString())); 
        tv.setText(i+""); 
       } catch (RemoteException e) { 
        e.printStackTrace(); 
       } 

      } 
     }); 
    } 
} 

和menifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.test.usingaidl"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service android:name=".AddService" android:process=":remoteService"> 
      <intent-filter > 
       <action android:name="com.test.usingaidl.addService"></action> 
      </intent-filter> 
     </service> 
    </application> 

</manifest> 

我有兩個EditText上與m到處兩個號碼,並使用服務將它們添加。 由於接口和服務位於同一個包中,因此它適用於此示例。但我想使用服務並在另一個活動中調用該方法。 我搜查了很多,但我發現的答案並沒有那麼有用。

回答

0

因此,經過很多搜索,我終於找到了答案。 問題是我必須在同一個包名中包含客戶端和服務器應用程序的aidl文件。

+0

你可以分享你的代碼片段如何實現這一目標,這樣對初學者會有幫助。 – vgokul129

相關問題