简体   繁体   中英

turn off screen during incoming call using broadcast receiver android

I want to prevent my android mobile screen from turning on while incoming call. I have searched so much but could not find any solution. I can turn off screen in my activity using 'system brightness' but not in broadcast receiver during incoming call. Anyone who has the solution? I am using following code in my activity and it works, but in broadcast receiver it's not working in onReceive method.

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);

Try the following code to turn off the screen instead of controling the brightness:

DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDPM.lockNow();
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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM