如何检测用户之间的切换 [英] How to detect switching between users
问题描述
我已经运行在前台模式下的服务,我想以检测运行Android 4.2或以上的平板电脑用户会话之间切换。
有没有广播接收机我可以注册得到通知?
我已经注意到,谷歌音乐,只要其他用户会话选择在锁定屏幕上停止音乐播放。它是如何检测开关?
答案解释
感谢@CommonsWare为正确答案。我将解释多一点如何检测用户开关。
首先要注意的文件明确表示,接收机必须通过 Context.registerReceiver
注册。因此做这样的事情:
UserSwitchReceiver接收器=新UserSwitchReceiver();
IntentFilter的过滤器=新的IntentFilter();
filter.addAction(Intent.ACTION_USER_BACKGROUND);
filter.addAction(Intent.ACTION_USER_FOREGROUND);
registerReceiver(接收机,过滤器);
然后在接收器还可以检索用户ID。这里是一个小片段:
公共类UserSwitchReceiver扩展的BroadcastReceiver {
私有静态最后字符串变量=UserSwitchReceiver;
@覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{
布尔userSentBackground = intent.getAction()等于(Intent.ACTION_USER_BACKGROUND);
布尔userSentForeground = intent.getAction()等于(Intent.ACTION_USER_FOREGROUND);
Log.d(TAG,交换机接收用户发送的背景=+ userSentBackground +用户发送的前景=+ userSentForeground +;);
INT用户= intent.getExtras()调用getInt(android.intent.extra.user_handle);
Log.d(TAG,用户=+用户);
}
}
尝试<一href="http://developer.android.com/reference/android/content/Intent.html#ACTION_USER_FOREGROUND"><$c$c>ACTION_USER_FOREGROUND$c$c>和<一href="http://developer.android.com/reference/android/content/Intent.html#ACTION_USER_BACKGROUND"><$c$c>ACTION_USER_BACKGROUND$c$c>.我没有使用过他们,但他们分别加入API级别17,和他们的描述看起来可能会有所帮助。
I have a service running in foreground mode and I'd like to detect switching between user sessions on tablets running Android 4.2 or above.
Is there any broadcast receiver I can register to get notified?
I have noticed that Google Music stops the music playback as soon as another user session is chosen on the lock screen. How does it detect the switch?
ANSWER EXPLAINED
Thanks @CommonsWare for the correct answer. I will explain a bit more how to detect a user switch.
First be aware that the documentation explicitly says that receivers must be registered through Context.registerReceiver
. Therefore do something like:
UserSwitchReceiver receiver = new UserSwitchReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction( Intent.ACTION_USER_BACKGROUND );
filter.addAction( Intent.ACTION_USER_FOREGROUND );
registerReceiver( receiver, filter );
Then in the receiver you can also retrieve the user id. Here is a small snippet:
public class UserSwitchReceiver extends BroadcastReceiver {
private static final String TAG = "UserSwitchReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
boolean userSentBackground = intent.getAction().equals( Intent.ACTION_USER_BACKGROUND );
boolean userSentForeground = intent.getAction().equals( Intent.ACTION_USER_FOREGROUND );
Log.d( TAG, "Switch received. User sent background = " + userSentBackground + "; User sent foreground = " + userSentForeground + ";" );
int user = intent.getExtras().getInt( "android.intent.extra.user_handle" );
Log.d( TAG, "user = " + user );
}
}
Try ACTION_USER_FOREGROUND
and ACTION_USER_BACKGROUND
. I have not used them, but they were added in API Level 17, and their description seems like it may help.
这篇关于如何检测用户之间的切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!