android修改系统源码之实现蓝牙自动配对以及取消开机锁屏

最近一段时间在公司项目开发中,遇到了使用蓝牙进行数据传输,但是还必须要求其中的一个搭载android系统的蓝牙设备默认接受所有来进行蓝牙连接的设备。我们知道在如今的android系统中,使用蓝牙进行配对时系统会随机生成的配对码并以对话框的形式展现给用户,让用户来选择是否可以进行配对。所以在尝试在应用APP上实现比较困难的情况下,转向了修改android源码实现该功能的方法。

首先我的android源码是基于android4.4.2的,蓝牙配对的那个java文件为:BluetoothPairingDialog.java,相应的路径为:CQA31SAndroid_4.4.2\android4.4\packages\apps\Settings\src\com\android\settings\bluetooth。我们打开该文件之后,阅读下该文件的代码,发现其结构也很简单,使用配对码进行配对的执行方法为:第255行的createConfirmationDialog(CachedBluetoothDeviceManager deviceManager)方法,在该方法中系统主要是进行了构建Dialog的过程,而这个Dialog就是要展现给我们的配对确认的Dialog。所以我们只要捕获到该对话框的“配对”或者“取消”按钮,就可以直接的控制系统的配对操作。

接下来我们先来看看该段源码:

private void createConfirmationDialog(CachedBluetoothDeviceManager deviceManager) {
		final AlertController.AlertParams p = mAlertParams;
        p.mIconId = android.R.drawable.ic_dialog_info;
	    p.mTitle = getString(R.string.bluetooth_pairing_request);
        p.mView = createView(deviceManager);
        p.mPositiveButtonText = getString(R.string.bluetooth_pairing_accept);
        p.mPositiveButtonListener = this;
        p.mNegativeButtonText = getString(R.string.bluetooth_pairing_decline);
        p.mNegativeButtonListener = this;
        setupAlert();
		
    }


 
那么我们该怎么捕获到这个Dialog的“配对”或者是“取消”按钮呢?我们知道对于AlertDialog来说,我们可以通过alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);来得到“确认”按钮的实例,然而在该源码处没有出现AlertDialog的实例对象,那我们就没有办法了么?答案当然是否定的!如果我们再深一层去查看下alertDialog.getButton方法就会发现该方法中是这样写的: 

    /**
     * Gets one of the buttons used in the dialog.
     * <p>
     * If a button does not exist in the dialog, null will be returned.
     * 
     * @param whichButton The identifier of the button that should be returned.
     *            For example, this can be
     *            {@link DialogInterface#BUTTON_POSITIVE}.
     * @return The button from the dialog, or null if a button does not exist.
     */
    public Button getButton(int whichButton) {
        return mAlert.getButton(whichButton);
    }
而这个mAlert就是AlertController类的实例化对象,这样一来我们就知道如何来实现了:首先在该类中声明两个变量,另外在createConfirmationDialog方法中进行捕获相应按键的Button对象,之后使用该button对象让它自己去触发点击事件就好了,具体代码如下:

声明2个button变量:

private Button mPositiveButton;
private Button mConsentButton;

在createConfirmationDialog中添加如下代码(仅仅添加了“配对”按钮):

mPositiveButton=mAlert.getButton(BUTTON_POSITIVE);
mPositiveButton.performClick();
之后进行保存,替换到之前的文件,重新编译运行即可。



取消开机自动锁屏

设置默认锁屏时间

相关路径:frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java

DatabaseHelper文件中找到如下方法片段:

private void loadSystemSettings(SQLiteDatabase db) {
loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
      R.integer.def_screen_off_timeout);
该方法设置了默认屏幕关闭时间,在def_screen_off_timeout中系统设置了6000毫秒,我们要做的就是要将6000改为-1即可

相应的xml文件路径为:frameworks/base/packages/SettingsProvider/res/values/defaults.xml

修改如下:

<integer name="def_screen_off_timeout">-1</integer>
这样修改的作用就是在开机后会进入一次锁屏,解锁之后就再也不会锁屏了。

接下里我们来解决开机不锁屏的功能:

相关路径:frameworks/base/policy/src/com/android/internal/policy/impl/KeyguardViewMediator.java

在该文件中找到

    /**
     * External apps (like the phone app) can tell us to disable the keygaurd.
     */
    private boolean mExternallyEnabled = true;
之后将true改为false,保存进行替换,之后重新编译就大功告成了!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值