[英]Android Facebook lock screen notification
在Android应用程序的最新版本Facebook上显示锁定屏幕通知功能,就像在这个截图上:
有没有人试图实现这个?
我知道在锁定屏幕上显示Activity很简单,但遗憾的是它不适用于半透明背景。 基本上它工作,但在我们的活动下面,我们看到启动器屏幕,而不是锁定屏幕(在这种情况下像锁屏也将是透明的)。
我现在尝试的是:
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发布之前创建这种通知。
实际上, ferdy182
正在某事上。
这是我使用android.permission.SYSTEM_ALERT_WINDOW
得到的:
所以,我无法使用Activity
执行此操作。 它只是行不通。 我必须实现一个使用WindowManager
添加View
的Service
。
一个可能的工作流程是: BroadcastReceiver
接收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" />
我认为它可能与Messenger的聊天头泡沫使用相同的技巧。
基本上,您使用此权限“android.permission.SYSTEM_ALERT_WINDOW”来显示您的视图高于其他应用程序。
我没有试过自己,但我很确定他们用过这个。
从文档“允许应用程序使用类型TYPE_SYSTEM_ALERT打开窗口,显示在所有其他应用程序之上。” http://developer.android.com/reference/android/Manifest.permission.html#SYSTEM_ALERT_WINDOW
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.