我正在使用链接http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/中的教程,并尝试在 android 中打开和关闭屏幕数据电话。但是我想出了The method onpause is undefined for the type object
,方法中发生了同样的错误OnResume
。我将android级别设置为10-17。
这是完整的代码:
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();
}
}