Pij*_*usn 0 java android broadcastreceiver
我正在尝试让我的应用程序在打开屏幕时打印一些内容,但它不能像我预期的那样工作.
这是我在Manifest文件中的内容
<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>
Run Code Online (Sandbox Code Playgroud)
我的接收器看起来像
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
}
Run Code Online (Sandbox Code Playgroud)
}
但它没有打印到日志.我一直按下我的Android电源按钮,屏幕打开/关闭,但我的消息没有出现在日志中.我错过了什么?它看起来与我在网上找到的例子相同.
您无法ACTION_SCREEN_ON通过BroadcastReceiver清单中注册的广播来收听广播.您必须通过registerReceiver()正在运行的组件进行注册.有这种特性的广播很少(ACTION_SCREEN_OFF,ACTION_BATTERY_CHANGED也许ACTION_USER_PRESENT).