Android 远程服务通信—AIDL

用本期代码控制上期Service代码      

网址:https://blog.csdn.net/qq_41664272/article/details/88584328

AIDL工程的主函数代码

package com.example.aidldemo;

import android.content.ComponentName;
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.util.Log;
import android.view.View;

import com.example.servicedemo.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            IMyAidlInterface imai = IMyAidlInterface.Stub.asInterface(iBinder);
            try {
                imai.showProgress();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void operate(View v){
        switch(v.getId()){
            case R.id.start:
                //远程启动服务
                Intent it = new Intent();
                it.setAction("com.imooc.myservice");
                it.setPackage("com.example.servicedemo");
                startService(it);
                break;
            case R.id.stop:
                //远程停止服务
                Intent it2 = new Intent();
                it2.setAction("com.imooc.myservice");
                it2.setPackage("com.example.servicedemo");
                stopService(it2);
                break;
            case R.id.bind:
                //远程绑定服务
                Intent it3 = new Intent();
                it3.setAction("com.imooc.myservice");
                it3.setPackage("com.example.servicedemo");
                bindService(it3,conn,BIND_AUTO_CREATE);
                break;
            case R.id.unbind:
                //远程解绑服务
                unbindService(conn);
                break;

        }
    }
}

接收代码如下

package com.example.aidldemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.internal.telephony.ITelephony;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class PhoneReceiver extends BroadcastReceiver {
    public PhoneReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.e("TAG","来电话了");
        //1.获取电话服务
        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        //2.读取状态
        switch(manager.getCallState()){
            case TelephonyManager.CALL_STATE_RINGING:
                //响铃
                String number = intent.getStringExtra("incoming_number");
                Log.e("TAG","响铃" + number);

                //ITelephony
                //ITelephony it = new I
                //endCall();

                // private ITelephony getITelephony()
                //反射
                //1.获取TelephonyManager类对象
                Class cls = TelephonyManager.class;

                //2.根据方法名获取到对应的方法
                try {
                    Method m = cls.getDeclaredMethod("getITelephony");
                    //3.设置方法可以被操作,执行方法,获取Itelephony返回值
                    m.setAccessible(true);
                    ITelephony it = (ITelephony) m.invoke(manager);
                    //4.调用endCall方法
                    it.endCall();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                //摘机
                Log.e("TAG","通过中");
                break;
            case  TelephonyManager.CALL_STATE_IDLE:
                //闲置
                break;
        }
    }
}

 

 

点击File里的New 里面的AIDL

新建的AIDL文件一定要Build一下

里面的AIDL必须与Service的位置以及内容相同,上期代码里没有。

代码如下

// IMyAidlInterface.aidl
package com.example.servicedemo;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);

     //定义自己所需要的方法:显示当前服务的进度
     void showProgress();
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context="com.example.aidldemo.MainActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="远程启动服务"
        android:onClick="operate" />

    <Button
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="远程停止服务"
        android:onClick="operate" />

    <Button
        android:id="@+id/bind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="远程绑定服务"
        android:onClick="operate" />

    <Button
        android:id="@+id/unbind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="远程解绑服务"
        android:onClick="operate"/>

</LinearLayout>

AndroidManifest

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

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

        <receiver
            android:name=".PhoneReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>

        </receiver>
    </application>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    </manifest>

猜你喜欢

转载自blog.csdn.net/qq_41664272/article/details/88669088