0

我正在使用链接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();
    }
}
4

2 回答 2

1

问题是您必须扩展 Activity(查看文章以确认这一点),您还必须覆盖每个继承的方法(使用 @Override 表示法)。

此外,您必须删除“registerReciever”,因为此调用是从 Activity 继承(并由其处理)的。

我已经发布了我认为正确的编码。

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();
}
}
于 2013-10-29T03:53:16.550 回答
1

首先,

public class ExampleActivity {
...

对于您在 Android 中定义的任何活动,您必须从 Activity 类扩展

public class ExampleActivity extends Activity {

OnCreate、OnResume 和 OnPause 是覆盖方法,因此请相应地更改它们

@Override
protected void onCreate() {
...

@Override
protected void onPause() {
...

@Override
protected void onResume() {
...
于 2013-10-29T03:46:55.903 回答