我有一个通过服务创建系统警报窗口的应用程序。
在select ISNULL([case owner], 'TOTAL') as AGENT,
SUM(CASE WHEN [final status]='CLOSED' THEN 1 ELSE 0 END) [CLOSED],
SUM(CASE WHEN [final status]='OPEN' THEN 1 ELSE 0 END) [OPEN],
SUM(CASE WHEN [final status]='REASSIGNED' THEN 1 ELSE 0 END) [REASSIGNED]
from StatusTable
GROUP BY ROLLUP ([case owner]);
我实例化意图:
MainActivity
在TintOverlayService中,我正在创建系统警报窗口,如下所示:
i = new Intent(getApplicationContext(), TintOverlayService.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
TintOverlayService的onStartCommand重写返回:
LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
mTopView = (ViewGroup) li.inflate(R.layout.red_overlay, null);
mTopView.setBackgroundColor(Color.parseColor(colorCode.replace("#", tintValue)));
DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
int LayoutParamFlags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
densityDpi * 6,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
LayoutParamFlags,
PixelFormat.TRANSLUCENT);
这样可以在屏幕上成功创建完整的叠加层。即使在应用程序之外,也是如预期的那样。
但是,当您激活叠加层并终止该应用时。叠加层闪烁一两秒钟然后重新启动。或者,如果您只是正常使用手机浏览互联网或观看视频......覆盖图将在几秒到30秒之间关闭,然后重新开启。
任何指出我正确方向的想法都会有所帮助。我一直试图解决这个问题。
答案 0 :(得分:0)
当您杀死该应用时,该服务也会被杀死。将START_STICKY设置为返回值后,系统将尝试尽快再次启动服务(当内存足够时)。 这导致服务再次创建叠加层。 如果你想阻止它,那么将返回值更改为START_NOT_STICKY就足够了。
杀死应用程序部分应用程序的功能还是只是你压力测试它?
修改强> 消失并再次出现可能是系统停止服务的结果(认为不再需要),并且START_STICKY导致服务再次启动。 我每隔X分钟就触发一次服务,从而规避了类似的问题。以下代码只是从内存中编写而我现在无法测试,因此可能包含一些错误。
public class OverlayService extends Service{
Handler handler;
Runnable runnable;
boolean isKeepAliveActive = false;
@Override
public void onCreate(){
//DO YOUR STUFF
initKeepAlive();
startKeepAlive();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction() == null ? "empty": intent.getAction();
if (action.equals("KeepAlive")){
//KEEP ALIVE TRIGGER, NOTHING TO DO
return START_NOT_STICKY;
}
else if (action.equals("WHATEVER")){
//DO SOMETHING ELSE
}
//DO YOUR STUFF
return START_NOT_STICKY;
}
@Override
public void onDestroy(){
stopKeepAlive();
super.onDestroy();
}
private void initKeepAlive(){
handler = new Handler();
runnable = new Runnable(){
@Override
private void run(){
try{
isKeepAliveActive = true;
//Get context of your service and the class name of your service
Intent intent = new Intent(context, OverlayService.class);
intent.setAction("KeepAlive");
context.startService(intent);
handler.postDelayed(runnable, 20*60*1000);
} catch (Exception e){
isKeepAliveActive = false;
}
}
private void startKeepAlive(){
if (!isKeppAliveActive){
runnable.run();
}
private void stopKeepAlive(){
if (isKeepAliveActive){
handler.removeCallbacks(runnable);
isKeepAliveActive = false;
}
}
}