使用Aidl在服务中调用另一个应用程序中的方法 [英] Calling a method in service which is in another app using aidl
问题描述
我正在遵循Android开发者手册中描述的方法. 这是我的辅助界面
I am following the method that is described in Android Developer's Cookbook. Here is my aidl interface
package com.test.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
My 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();
}
}
});
}
}
和最明显的
<?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获得两个数字,并使用service将它们相加. 由于接口和服务在同一软件包中,因此对于本示例来说效果很好.但是我想使用服务并在另一个活动中调用该方法. 我进行了很多搜索,但发现的答案并没有那么大的帮助.
I have two EditText and m getting two numbers and adding them using service. It works fine for this example as the interface and service is in the same package. But I want to use service and call the method in another activity. I have searched a lot but the answers I found were not so helpful.
推荐答案
因此,经过大量搜索,我终于找到了答案. 问题是我必须在相同的程序包名称中同时拥有客户端和服务器应用程序的辅助文件.
So after lot of search I finally have found the answer. The problem was that I had to have the aidl file of both of the client and server application in the same package name.
这篇关于使用Aidl在服务中调用另一个应用程序中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!