您的位置:首页 > 大数据 > 人工智能

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

2017-03-15 14:14 1346 查看
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();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐