Android 通话接通3s后灭屏

通话状态监听类:
vendor/mediatek/proprietary/packages/services/Telecomm/src/com/android/server/telecom/PhoneStateBroadcaster.java

  • 构造方法中初始化PMS
+	private IPowerManager mPowerMangerService;

    public PhoneStateBroadcaster(CallsManager callsManager) {
        mCallsManager = callsManager;
        mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
                "telephony.registry"));
        if (mRegistry == null) {
            Log.w(this, "TelephonyRegistry is null");
        }
		
+		mPowerMangerService = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));///mh.add
    }
  • 监听电话状态,控制屏幕亮灭
    @Override
    public void onCallStateChanged(Call call, int oldState, int newState) {
        if (call.isExternalCall()) {
            return;
        }
        updateStates(call);
		
+		updateScreenState(newState);///mh.add
    }
    
+	///mh.add
+	Runnable timeOutTask = new Runnable() {
+		public void run() {
+			try{
+				mPowerMangerService.goToSleep(SystemClock.uptimeMillis(),
+								PowerManager.GO_TO_SLEEP_REASON_APPLICATION, 0);
+			}catch(RemoteException e) {
+
+			}
+		}
+	};

+	private void updateScreenState(int newState) {
+		if(newState == CallState.ACTIVE) {
+			mHandler.removeCallbacks(timeOutTask);
+			mHandler.postDelayed(timeOutTask, 3000);///定时器
+		} else {
+			mHandler.removeCallbacks(timeOutTask);
+		}
+	}
+	///mh.end
  • 导入相关包
	+import android.os.Handler;
	+import android.os.IPowerManager;
	+import android.os.PowerManager;
	+import android.os.SystemClock;

你可能感兴趣的:(android客制,android,java)