我试图让我的应用程序在屏幕打开时打印一些内容来记录,但它没有按我的预期工作。
这是我的 list 文件中的内容
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity ...>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="PhoneBroadcastReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"></action>
</intent-filter>
</receiver>
</application>
我的接收器看起来像
public class PhoneBroadcastReceiver extends BroadcastReceiver {
public PhoneBroadcastReceiver()
{
}
@Override
public void onReceive(Context _context, Intent _intent) {
// TODO Auto-generated method stub
String a = _intent.getAction();
MessageHandler.log("Received action: " + a); // just a wrapper for printing to a log
}
}
但它不会在日志中打印任何内容。我一直按 Android 电源按钮,屏幕打开/关闭,但我的消息没有出现在日志中。我缺少什么?它看起来与我在网上找到的示例相同。
最佳答案
您无法通过 list 中注册的 BroadcastReceiver
监听 ACTION_SCREEN_ON
广播。您必须从正在运行的组件通过 registerReceiver()
注册它。具有此特征的广播相当少(ACTION_SCREEN_OFF
、ACTION_BATTERY_CHANGED
,也许还有 ACTION_USER_PRESENT
)。
关于java - Android 广播接收器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6698716/