Reputation: 375
I am using the tutorial from the link http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/ and try to get the screen on and off data in android phone. But I come up with the The method onpause is undefined for the type object
, the same error happens in the OnResume
method. I set the android level from 10-17.
Here is the whole code for that:
package com.example.myfirstapp;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
public class ExampleActivity {
protected void onCreate() {
// INITIALIZE RECEIVER
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
ScreenReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
// YOUR CODE
}
private void registerReceiver(ScreenReceiver mReceiver, IntentFilter filter) {
// TODO Auto-generated method stub
}
protected void onPause() {
// WHEN THE SCREEN IS ABOUT TO TURN OFF
if (ScreenReceiver.wasScreenOn) {
// THIS IS THE CASE WHEN ONPAUSE() IS CALLED BY THE SYSTEM DUE TO A SCREEN STATE CHANGE
System.out.println("SCREEN TURNED OFF");
} else {
// THIS IS WHEN ONPAUSE() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
}
super.onPause();
}
protected void onResume() {
// ONLY WHEN SCREEN TURNS ON
if (!ScreenReceiver.wasScreenOn) {
// THIS IS WHEN ONRESUME() IS CALLED DUE TO A SCREEN STATE CHANGE
System.out.println("SCREEN TURNED ON");
} else {
// THIS IS WHEN ONRESUME() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
}
super.onResume();
}
}
Upvotes: 0
Views: 989
Reputation: 2630
The issues is that you have to extend Activity (check the article to confirm this), you also have to Override each of the inherited methods (using the @Override notation).
Also, you have to remove "registerReciever" as this call is inherited from (and handled by) the Activity.
I have posted what I believe is the correct coding.
package com.example.myfirstapp;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
public class ExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// INITIALIZE RECEIVER
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
ScreenReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
// YOUR CODE
}
@Override
protected void onPause() {
// WHEN THE SCREEN IS ABOUT TO TURN OFF
if (ScreenReceiver.wasScreenOn) {
// THIS IS THE CASE WHEN ONPAUSE() IS CALLED BY THE SYSTEM DUE TO A SCREEN STATE CHANGE
System.out.println("SCREEN TURNED OFF");
} else {
// THIS IS WHEN ONPAUSE() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
}
super.onPause();
}
@Override
protected void onResume() {
// ONLY WHEN SCREEN TURNS ON
if (!ScreenReceiver.wasScreenOn) {
// THIS IS WHEN ONRESUME() IS CALLED DUE TO A SCREEN STATE CHANGE
System.out.println("SCREEN TURNED ON");
} else {
// THIS IS WHEN ONRESUME() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
}
super.onResume();
}
}
Upvotes: 1
Reputation: 7030
First off,
public class ExampleActivity {
...
For any activities you define in Android, you must extend from the Activity class
public class ExampleActivity extends Activity {
OnCreate, OnResume and OnPause are override methods, so change them accordingly
@Override
protected void onCreate() {
...
@Override
protected void onPause() {
...
@Override
protected void onResume() {
...
Upvotes: 1