我有以下问题
答案 0 :(得分:0)
为要通过IBinder传递的对象创建.aidl文件。您应该在与其定义的Java对象相同的包中创建.aidl文件。
Google Developer, Android Interface Definition Language (AIDL)
答案 1 :(得分:0)
以下是我的回答:
没有。创建文件后,您应该按照以下步骤操作:
一个。在.aidl中定义API
湾创建从Android服务类
扩展的服务℃。创建一个实现XXX.Stub的实例(XXX是你的.aidl文件名)
d。绑定您的服务
希望它有所帮助。
答案 2 :(得分:0)
/********************Application 1 Isum.aidl*****************
package com.example.Isum;
interface Isum{
int sum(int a, int b);
}
/***********************Application 1*********Don't do anything in main
/********create service class*********remote service
public class RemoteService extends Service{
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate(){
super.onCreate();
}
@Override
public void onDestroy(){
super.onDestroy();
}
private final Isum.Stub mBinder=new Isum.Stub() {
@Override
public int sum(int a, int b) throws RemoteException {
int c=a+b;
return c;
}
};
**********************Application 2 Main activity*********
public class MainActivity extends Activity {
private Isum msum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSum=(Button)findViewById(R.id.btnResult);
btnSum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
bindService(new Intent("com.example.serviceprovider.RemoteService"),
mConnection, Context.BIND_AUTO_CREATE);
EditText FirstNo=(EditText)findViewById(R.id.ETfirstNo);
EditText SecondNo=(EditText)findViewById(R.id.ETSecondNo);
int a=Integer.parseInt(FirstNo.getText().toString());
System.out.println("###########"+a);
int b=Integer.parseInt(SecondNo.getText().toString());
try {
int c=msum.sum(a, b);
System.out.println("***********************"+c);
Toast.makeText(MainActivity.this, c, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private ServiceConnection mConnection=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
msum=Isum.Stub.asInterface(arg1);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
msum=null;
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
/****************include this in mainfest file
<service android:name="RemoteService">
<intent-filter>
<action android:name="com.example.serviceprovider.RemoteService"/>
</intent-filter>
</service>
答案 3 :(得分:0)
public void onStart(){
super.onStart();
Intent intent=new Intent(MainActivity.this, RemoteService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
public void onServiceConnected(ComponentName className, IBinder service) {
mIsum=Isum.Stub.asInterface((IBinder)service);
mBound=true;
}