【发布时间】:2015-03-17 09:00:34
【问题描述】:
我在 Manifest 中声明了这个广播接收器:
<receiver android:name="classes.VoiceLaunchReceiver" >
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
我的接收者类:
public class VoiceLaunchReceiver extends android.content.BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
Intent service = new Intent(ctx, VoiceLaunchService.class);
// service.putExtra(action, true);
Log.i("joscsr","Incoming Voice Launch Broadcast...");
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Log.e("joshcsr", "***********\nCSR Resumed (BC)\n************");
ctx.startService(service);
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.e("joshcsr", "***********\nCSR STOPPED by SCREEN (BC)\n************");
ctx.stopService(service);
}
}
}
我在服务的 onCreate 方法中注册了 SCREEN_OFF 意图:
@Override
public void onCreate() {
super.onCreate();
//Register screen OFF BroadCast
IntentFilter mIntentFilter=new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(speechBroadcastReceiver, mIntentFilter);
}
只要用户解锁并打开屏幕,它就会做它的事情。它在 Jellybean 4.3 及更低版本中完美运行:只要屏幕被锁定或解锁,我的广播接收器内的 Logcats 就会显示。 为什么这个确切的代码在 Lollipop 中不起作用?有没有我必须听的新意图?
(我已经知道系统会发送那个意图,我想要的是用我的接收器检测它)
【问题讨论】:
-
也发布你的java代码
-
行不通?你的 logcat 中显示了什么?
-
@ShakeebAyaz 大家好,我添加了广播接收器的代码和 Screen_OFF 意图的注册代码。你可以看到我试图在 Lollipop 中显示的 logcat。
-
ACTION_SCREEN_OFF 仅在服务仍在运行时有效。检查您的服务是否被杀死或什么的。
-
@velis 我明白你的意思,但 Intent.ACTION_USER_PRESENT 一开始就没有被抓住。
标签: android android-intent broadcastreceiver android-5.0-lollipop