1

I want to find out that the screen is OFF when user locked a phone in android.I used BroadCast receiver like this and registered but I am not getting the screen is off or not

public class MyActivity extends Activity{   

    private static final String ACTION = "android.intent.action.ACTION_SCREEN_OFF";
    BroadcastReceiver myReceiver;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        context = this;
        final IntentFilter theFilter = new IntentFilter();
        theFilter.addAction(ACTION);

        context.registerReceiver(myReceiver, theFilter);
        System.out.println("inside increate");
        myReceiver = new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub      
                    System.out.println("phone locked*****");                
            }

        };   

    }}

How to find out this issue ?

Prasad
  • 1,375
  • 5
  • 17
  • 31

1 Answers1

2

Step #1: Delete the private static final String ACTION line

Step #2: Use Intent.ACTION_SCREEN_OFF in your addAction() line (or, better yet, just supply it to the IntentFilter constructor and get rid of the addAction() line)

Step #3: Use android.util.Log instead of System.out.println()

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491