Android Facebook锁屏通知 [英] Android Facebook lock screen notification
问题描述
在最新版本的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/>
另外我注意到锁屏通知会触发,所以我们无法通过手势显示状态栏的通知。 p>
任何想法如何在Android L发布之前创建这种通知。
p>实际上, ferdy182
是/在某事上。
这是我使用 android.permission.SYSTEM_ALERT_WINDOW
:
所以,我不能用活动
这样做。它不行。我必须执行一个服务
,它使用 WindowManager $ c $添加了一个
View
C>。
一个可能的工作流程是:您的 BroadcastReceiver收到广播
=>它启动一个服务=>服务添加了所需的视图。
现在,代码(意见解释了几件事):
public class MyService extends Service {
查看mView;
@Override
public IBinder onBind(Intent intent){
return null;
}
@Override
public void onCreate(){
super.onCreate();
// WindowManager的实例
WindowManager mWindowManager =(WindowManager)getSystemService(WINDOW_SERVICE);
LayoutInflater mInflater =(LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate必需的布局文件
mView = mInflater.inflate(R.layout.abc,null);
//附加OnClickListener
mView.findViewById(R.id.some_id).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//您可以相应地触发Intent - 处理点击事件
//停止服务 - 这也从窗口中删除mView
//因为onDestroy()被调用 - 这就是我们删除`mView`
stopSelf();
}
});
//`mView`的LayoutParams
//这里的主要吸引力是`TYPE_SYSTEM_ERROR`
//如上所述,`TYPE_SYSTEM_ALERT`在锁屏上无效
//`TYPE_SYSTEM_OVERLAY`工作得很好但是可以调整 - 没有点击事件
//`TYPE_SYSTEM_ERROR`支持所有这些要求
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);
//最后,将视图添加到窗口
mWindowManager.addView(mView,mLayoutParams);
}
@Override
public void onDestroy(){
super.onDestroy();
//从窗口中删除`mView`
removeViewFromWindow();
}
//从窗口中删除`mView`
public void removeNow(){
if(mView!= null){
WindowManager wm =(WindowManager)getSystemService(WINDOW_SERVICE);
wm.removeView(mView);
}
}
}
最后,添加权限到你的应用程序的清单:
< uses-permission android:name =android.permission.SYSTEM_ALERT_WINDOW/>
On the latest version of Android app Facebook showed lock screen notification feature, like on this screenshot:
Did anyone try to implement this?
I know that It's simple to show Activity on top of lock screen, but unfortunately It doesn't work with translucent background. Basically it works but below our activity we see launcher screen, not lock screen (like lock screen in this case would be also transparent).
What I tried right now is:
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);
in my Activity.
Also I tried this example: https://gist.github.com/daichan4649/5352944
And as I described - everything works but no transparency.
From my observation Facebook uses theme:
@android:style/Theme.Translucent.NoTitleBar
and doesn't have permission:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Also I noticed that lock screen notification aquires touches so we cannot show notifications from statusbar by gesture.
Any ideas how to create that kind of notification before Android L release.
Actually, ferdy182
was/is onto something.
Here's what I got using the android.permission.SYSTEM_ALERT_WINDOW
:
So, I couldn't do this with an Activity
. It just wouldn't work. I had to implement a Service
which added a View
using the WindowManager
.
One possible workflow would be: a broadcast is received by your BroadcastReceiver
=> it starts a Service => the Service adds the required view.
Now, the code (the comments explain a few things):
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);
}
}
}
And finally, add the permission to your app's manifest:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
这篇关于Android Facebook锁屏通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!