使用广播接收器android在来电期间关闭屏幕

时间:2014-09-18 05:46:22

标签: android

我希望防止我的Android手机屏幕在来电时打开。我搜索了这么多,但找不到任何解决方案。我可以使用“系统亮度”在我的活动中关闭屏幕,但在来电期间不能在广播接收器中关闭。谁有解决方案? 我在我的活动中使用以下代码并且它可以工作,但是在广播接收器中它不在onReceive方法中工作。

WindowManager.LayoutParams layoutParam = getWindow().getAttributes();
oldBrightness = android.provider.Settings.System.getInt(getContentResolver(),          
android.provider.Settings.System.SCREEN_BRIGHTNESS)/255f;
layoutParam.screenBrightness = 0; 
layoutParam.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(layoutParam);

2 个答案:

答案 0 :(得分:0)

尝试使用以下代码关闭屏幕而不是控制亮度:

DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDPM.lockNow();

答案 1 :(得分:0)

Have you declared broadcast receiver in manifest file.

Please check below code in your manifest file:

1.    

            <uses-permission android:name="android.permission.READ_PHONE_STATE" />

            <receiver android:name=".IncomingCallReceiver " android:enabled="true">
                <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
            </receiver> 

2.
Receiver File:IncomingCallReceiver.java

 public class IncomingCallReceiver extends BroadcastReceiver
 {
@Override
      public void onReceive(Context mContext, Intent intent)
      {

          Toast.makeText(mContext, "call is coming", Toast.LENGTH_SHORT).show();
}