我正在遵循Android Developer's Cookbook中描述的方法。 这是我的aidl界面
package com.test.aidl;
interface IMyAidl{
int add(int n1, int n2 );
}
我的serice课程
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;
}
};
}
我的主要活动
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();
}
}
});
}
}
和清单
<?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,我得到两个数字并使用服务添加它们。 它适用于此示例,因为接口和服务位于同一个包中。但我想使用服务并在另一个活动中调用该方法。 我搜索了很多,但我找到的答案并没有那么有用。
答案 0 :(得分:0)
经过大量的搜索,我终于找到了答案。 问题是我必须在同一个包名中拥有客户端和服务器应用程序的aidl文件。