Пытаюсь сделать так, чтобы при ответе на входящий звонок отключался экран
Использую PoweManager :
public void turnOffScreen(){
// turn off screen
Log.v("ProximityActivity", "OFF!");
mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "tag");
mWakeLock.acquire();
}
Ниже весь класс:
public class CallReceiver extends BroadcastReceiver {
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;
@Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final PhoneStateListener listener = new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(context.getApplicationContext(), "Call Ended..", Toast.LENGTH_LONG).show();
Log.i("stop", "Call Ended....");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context.getApplicationContext(), "Call Picked..", Toast.LENGTH_LONG) .show();
Log.i("received", "Call Picked....");
startRecording(incomingNumber);
//turnOffScreen();
break;
case TelephonyManager.CALL_STATE_RINGING:
// Вот здесь надо обрабатывать звонок
Toast.makeText(context.getApplicationContext(), "Call RingingBEOCH..." + incomingNumber,Toast.LENGTH_LONG).show();
autoPickCall(context);
/*try {
Runtime.getRuntime().exec("input keyevent " +
Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (IOException e) {
}*/
break;
}
}
};
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
При ответе на звонок приложение крашится Логи:
11-16 06:02:17.674 20512-20512/com.phonedialer.phonedialer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.phonedialer.phonedialer, PID: 20512
java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.PowerManager$WakeLock android.os.PowerManager.newWakeLock(int, java.lang.String)' on a null object reference
at com.test.myapplication.CallReceiver.turnOffScreen(CallReceiver.java:114)
at com.test.myapplication.CallReceiver$1.onCallStateChanged(CallReceiver.java:43)
at android.telephony.PhoneStateListener$1.handleMessage(PhoneStateListener.java:291)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:921)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:716)
Где ошибка может быть?`