一叶梧桐

解决:在低电量关机的同时自动挂断电话电话

首先我们通过定位找到低电量自动关机的代码:

相关类:service/java/com/android/server/BatteryService.java

相关方法:shutdownIfNoPowerLocked()

修改后代码(红色部分为关键代码):

private void shutdownIfNoPowerLocked() {

        // shut down gracefully if our battery is critically low and we are not powered.

        // wait until the system has booted before attempting to display the shutdown dialog.

        if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {

            mHandler.post(new Runnable() {

                @Override

                public void run() {

                    if (ActivityManagerNative.isSystemReady()) {

                        if (FeatureOption.MTK_IPO_SUPPORT == true) {

                            SystemProperties.set("sys.ipo.battlow","1");

                        }

                        try {

                            ITelephony iTelephony = ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));

                            if (iTelephony != null) {

                                iTelephony.endCall();

                            }

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                        Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);

                        intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);

                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                        mContext.startActivityAsUser(intent, UserHandle.CURRENT);

                    }

                }

            });

        }

    }

另外不要忘记引入相关库:

import com.android.internal.telephony.ITelephony;


posted on 2014-08-22 10:57  一叶梧桐  阅读(553)  评论(0编辑  收藏  举报