利用aidl技术和反射原理拿到自定义系统服务

该博客介绍了如何通过AIDL(Android Interface Definition Language)结合反射技术来获取并操作自定义的系统服务。在示例代码中,展示了如何创建服务请求,调用`IAcm100Service`接口的方法,并处理可能的异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package navigation.chiphd.com.acm100servicedemo;

import android.os.Bundle;
import android.os.IAcm100Service;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG ="MainActivity" ;

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

        Button bt = (Button) findViewById(R.id.bt);

        bt.setOnClickListener(this);


}

    @Override
    public void onClick(View v) {

        Class serviceManager = null;
        try {
            serviceManager = Class.forName("android.os.ServiceManager");

        Method method = serviceManager.getMethod("getService", String.class);

        Object  acm100 = method.invoke(serviceManager.newInstance(), "acm100");

        IAcm100Service asInterface = IAcm100Service.Stub.asInterface((IBinder) acm100);
            asInterface.setVal(1, 1);
            asInterface.setVal(9,1);
            asInterface.setVal(11,0xabcdef);
            asInterface.getVal(21);
            int val = asInterface.getVal(27);

            Log.e(TAG, "read CardNum=0x" + Integer.toHexString(val));

            Toast.makeText(MainActivity.this, val+"", Toast.LENGTH_SHORT).show();


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值