在最新版本的Android应用程序Facebook上,显示了锁屏通知功能,如以下屏幕截图所示:
有没有人尝试实现这一点?
我知道在锁定屏幕上显示活动很简单,但不幸的是它不能在半透明的背景下工作。基本上它可以工作,但在我们的活动下面,我们看到的是启动器屏幕,而不是锁屏(就像这种情况下的锁屏也是透明的)。
我现在尝试的是:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
在我的活动中。
我还尝试了这个例子:https://gist.github.com/daichan4649/5352944
正如我所描述的-一切都正常,但没有透明度。
根据我的观察,Facebook使用了主题:
@android:style/Theme.Translucent.NoTitleBar
并且没有权限:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
另外,我注意到锁屏通知需要触摸,所以我们不能通过手势显示来自状态栏的通知。
有什么办法在Android L发布之前创建这样的通知吗?
发布于 2014-08-06 20:23:29
实际上,ferdy182
曾经/正在做一些事情。
下面是我使用android.permission.SYSTEM_ALERT_WINDOW
得到的结果
所以,我不能用Activity
做到这一点。它就是不起作用。我必须实现一个Service
,它使用WindowManager
添加了一个View
。
一种可能的工作流程是:广播由您的BroadcastReceiver
=>接收,它启动一个服务=>服务添加所需的视图。
现在,代码(注释解释了一些事情):
public class MyService extends Service {
View mView;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
// instance of WindowManager
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater mInflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate required layout file
mView = mInflater.inflate(R.layout.abc, null);
// attach OnClickListener
mView.findViewById(R.id.some_id).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// you can fire an Intent accordingly - to deal with the click event
// stop the service - this also removes `mView` from the window
// because onDestroy() is called - that's where we remove `mView`
stopSelf();
}
});
// the LayoutParams for `mView`
// main attraction here is `TYPE_SYSTEM_ERROR`
// as you noted above, `TYPE_SYSTEM_ALERT` does not work on the lockscreen
// `TYPE_SYSTEM_OVERLAY` works very well but is focusable - no click events
// `TYPE_SYSTEM_ERROR` supports all these requirements
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
PixelFormat.RGBA_8888);
// finally, add the view to window
mWindowManager.addView(mView, mLayoutParams);
}
@Override
public void onDestroy() {
super.onDestroy();
// remove `mView` from the window
removeViewFromWindow();
}
// Removes `mView` from the window
public void removeNow() {
if (mView != null) {
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeView(mView);
}
}
}
最后,将权限添加到应用程序的清单中:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
发布于 2014-08-01 17:47:27
我认为它可能使用了与Messenger的聊天头气泡相同的技巧。
基本上,你使用这个权限"android.permission.SYSTEM_ALERT_WINDOW“在其他应用程序之上显示你的视图。
我还没试过,但我很确定他们用的是这个。
http://developer.android.com/reference/android/Manifest.permission.html#SYSTEM_ALERT_WINDOW“允许应用程序使用TYPE_SYSTEM_ALERT类型打开窗口,显示在所有其他应用程序的顶部。”
https://stackoverflow.com/questions/24921241
复制相似问题