0

I am working on android app in which I want to turn device screen off during any incoming call.

I am using the following code to detect an incoming call.

public class MyCallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_RINGING)) {
            // This code will execute when the phone has an incoming call

            // get the phone number
            String incomingNumber = intent
                    .getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Toast.makeText(context, "Call from:" + incomingNumber,
                    Toast.LENGTH_LONG).show();

            // here I need to setting device screen off

        } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_IDLE)
                || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                        TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            // This code will execute when the call is disconnected
            Toast.makeText(context, "Detected call hangup event",
                    Toast.LENGTH_LONG).show();

        }
    }
}

I searched and got this code which turns off the device screen. I implemented it inside my code, but without success and I cannot turn the device screen off programmatically during an incoming call yet.

Can anybody help me regarding this?

Edit:
The manifest file is like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

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


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.demo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.demo.MyCallReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
Community
  • 1
  • 1
DreamsNeverDie
  • 550
  • 1
  • 6
  • 14
  • Downvoter can you please write comment what is wrong in my question ? – DreamsNeverDie Mar 29 '14 at 15:40
  • Have you checked if you need any specific permissions setting in Manifest to allow you to do this? Certainly on earlier versions of android, if correct permissions were not set the calls to hardware etc. would fail silently. – D-Dᴙum Mar 29 '14 at 16:13
  • And are you sure you are receiving the broadcast? Put a call into `Log.i()` in your broadcast receiver and look at the output of `logcat` – D-Dᴙum Mar 29 '14 at 16:17
  • @Kerry yes I added permissions in manifest file. I edited my question please look. And yes I am put Log.i() inside broadcast receiver its working perfectly. – DreamsNeverDie Mar 29 '14 at 17:59
  • You haven't show the actual code that you are using to switch the screen off though. What specific Android call? – D-Dᴙum Mar 30 '14 at 08:49
  • I tried this code : http://stackoverflow.com/questions/6756768/turn-off-screen-on-android/6757206#6757206 – DreamsNeverDie Mar 30 '14 at 08:52
  • @Kerry Can you please help me. – DreamsNeverDie Mar 30 '14 at 17:12

0 Answers0