tags:

views:

50

answers:

1

I have the following code I want to detect the screen off of the phone But the function onReceive never is never called can anyone give me a solution. Please help. if anyone has anyother working code please help. Thank you

package com.pack;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class Screen extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
     Context con = this;
        this.registerReceiver(new BroadcastReceiver() {

              @Override
              public void onReceive(Context context, Intent intent) {
               System.out.println("Screen off...................");
              }
            }, new IntentFilter(Intent.ACTION_SCREEN_OFF));
    }

}
+1  A: 

Your code works for me. I modified the code so it's easier to read and added some sysouts, but it works the way you wrote it:

public class Test extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println("Screen off...................");
        }

    }, filter);
}

@Override
protected void onResume() {
    super.onResume();
    System.out.println("onResume()");
}

@Override
protected void onDestroy() {
    super.onDestroy();
    System.out.println("onDestroy()");
}

@Override
protected void onPause() {
    super.onPause();
    System.out.println("onPause()");
}

It prints the following if I do (start activity, turn screen off, turn screen on, press back button):

onResume()
onPause()
Screen off...................
onResume()
onPause()
onDestroy()

After this the activity is destroyed and therefore no events are displayed. Maybe you want to register your BroadcastReceiver in the AndroidManifest instead.

mreichelt
I got only onResume as output. I am using 1.5 Is that the reason> here is my manifest file <intent-filter> <action android:name="android.intent.action.SCREEN_OFF"></action> </intent-filter> <intent-filter> <action android:name="android.intent.action.SCREEN_ON"></action> </intent-filter>
Jim
The above code calls onResume,onDestroy and onPause for me but notonReceive why what is wrong with 'my' code. It is same as the above
Jim
Interesting - normally your code should just work. Do you test on a real device? Or in an emulator? Please provide more information.
mreichelt
OH sorry Actually I want to be notified when the phone's backlight becomes off(not switch off).i.e I want to get a function to be called when the phones's backlight goes off.
Jim
Ok - I think there is no intent regarding a change of the brightness. However you might read the current brightness by using the class Settings.System: http://developer.android.com/reference/android/provider/Settings.System.html
mreichelt