为什么这个广播接收器不适用于Android Lollipop?

时间:2015-03-17 09:00:34

标签: android android-intent broadcastreceiver android-5.0-lollipop

我在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都会显示。 为什么这个确切的代码不会在棒棒糖中起作用?我必须听取新的意图吗?

(我已经知道系统发送了这个意图,我想要的是用接收器检测它)

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我修复了它(在4.3和5.1上测试过)。我能够在清单中声明"android.intent.action.USER_PRESENT",只要你拥有READ_PHONE_STATE权限,就可以!我的迷你应用程序包括一个广播接收器,它响应屏幕的开/关状态,并运行一个连续语音识别的后台服务。如果屏幕关闭,则关闭识别。

以下是代码,享受:MANIFEST:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <receiver android:name="classes.VoiceLaunchReceiver" >
            <intent-filter>                
                <action android:name="android.intent.action.USER_PRESENT" />    
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

广播接收器:

public class VoiceLaunchReceiver extends 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.i("joshcsr", "************\nCSR Resumed (BC)\n************");
            ctx.startService(service);
            }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.i("joshcsr", "************\nCSR STOPPED by SCREEN (BC)\n************");
            ctx.stopService(service);  
            }
        }  
}

您可以想象,我的USER_PRESENT广播接收器未在其他任何地方注册。我在我的服务的ACTION_SCREEN_OFF方法中注册onCreate和ON,由我的接收者触发。

@Override
public void onCreate() {
    super.onCreate();
    //Register screen ON/OFF BroadCast
    launcher=new VoiceLaunchReceiver();
    IntentFilter i=new IntentFilter(Intent.ACTION_SCREEN_OFF);
    i.addAction(Intent.ACTION_SCREEN_ON);               
    registerReceiver(launcher,i);
    Log.d("joshcsr","VoiceLaunch Service CREATED"); 
    }

最后,我在我的服务的onDestroy()中取消注册屏幕:

@Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(launcher);}