Android 我从未打过电话
我有以下代码 我想检测一下手机的屏幕 但从未调用onReceive never函数 谁能给我一个解决办法。 请帮忙。 如果任何人有任何其他工作代码,请帮助。 多谢各位Android 我从未打过电话,android,Android,我有以下代码 我想检测一下手机的屏幕 但从未调用onReceive never函数 谁能给我一个解决办法。 请帮忙。 如果任何人有任何其他工作代码,请帮助。 多谢各位 package com.pack; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import androi
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));
}
}
你的代码对我有用。我修改了代码,使其更易于阅读,并添加了一些sysout,但它的工作方式与您编写的方式相同:
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()");
}
如果我这样做,它会打印以下内容(启动活动、关闭屏幕、打开屏幕、按后退按钮):
在此之后,活动将被销毁,因此不会显示任何事件。也许你想在AndroidManifest中注册你的BroadcastReceiver。我只得到了onResume作为输出。我使用1.5的原因是>这是我的清单文件,上面的代码为我调用了onResume、onDestroy和onPause,但没有接收到“我的”代码有什么问题。这与上面有趣的代码相同——通常您的代码应该可以正常工作。你在真正的设备上测试吗?还是在模拟器中?请提供更多信息。哦,对不起,事实上,我希望在手机背光关闭(而不是关闭)时收到通知。也就是说,我希望在手机背光关闭时调用一个功能。好的-我认为没有改变亮度的意图。但是,您可以使用类别设置读取当前亮度。系统:
onResume()
onPause()
Screen off...................
onResume()
onPause()
onDestroy()