Calling a method in service which is in another app using aidl

社会主义新天地 提交于 2020-01-07 03:40:40

问题


I am following the method that is described in Android Developer's Cookbook. Here is my aidl interface

package com.test.aidl;

interface IMyAidl{

    int add(int n1, int n2 );
}

My serice class

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;
        }
    };


}

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();
                }

            }
        });
    }
}

and the 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>

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.


回答1:


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.



来源:https://stackoverflow.com/questions/34892048/calling-a-method-in-service-which-is-in-another-app-using-aidl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!