Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android系统警报窗口定期关闭和打开_Android_Android Intent_Android Service_Android Intentservice - Fatal编程技术网

Android系统警报窗口定期关闭和打开

Android系统警报窗口定期关闭和打开,android,android-intent,android-service,android-intentservice,Android,Android Intent,Android Service,Android Intentservice,我有一个通过服务创建系统警报窗口的应用程序 在MainActivity中,我实例化了意图: i = new Intent(getApplicationContext(), TintOverlayService.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 在TintOverlyService中,我正在创建系统警报窗口,如下所示: LayoutInflater li = (

我有一个通过服务创建系统警报窗口的应用程序

MainActivity
中,我实例化了意图:

i = new Intent(getApplicationContext(), TintOverlayService.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
在TintOverlyService中,我正在创建系统警报窗口,如下所示:

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);
而TintOverlayService的OnStart命令覆盖将返回:

return START_STICKY;
这成功地在屏幕上创建了一个完整的边缘覆盖。即使在应用程序之外,也可以按预期使用

但是,当您激活覆盖并关闭应用程序时。覆盖层闪烁一两秒钟,然后再次启动。或者,如果你只是正常使用手机浏览互联网或观看视频。。。覆盖层将在几秒到30秒之间关闭,然后重新打开


任何给我指出正确方向的想法都会很有帮助。我一直想弄明白这一点已经有一段时间了

当你关闭应用程序时,服务也会被关闭。将START_STICKY设置为返回值后,系统将尽快尝试再次启动服务(当它有足够的内存时)。 这将导致服务再次创建覆盖。 如果要防止出现这种情况,将返回值更改为START\u NOT\u 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;
    }
}
}

杀死它只是压力测试。我认为在关闭应用程序时就是这样。最让我担心的是,当你正常使用手机时,覆盖层会被破坏并再次启动。我不确定是否有办法修补它,或者只是一个愚蠢的安卓系统。你想要什么样的行为?如果在应用程序被终止/崩溃时覆盖层应该保持删除状态,则START\u NOT\u STICKY应该可以解决此问题。我希望它保持不变。问题是它会定期关闭和重新打开。即使没有关闭应用程序。因此,你打开覆盖并切换到其他应用程序或开始浏览互联网。最终,覆盖将打开和关闭。我编辑了我的答案。这种行为最有可能来自于系统停止(假定)空闲服务,然后由于START\u STICKY.Awesome而重新启动服务!今晚我会试试这个,让你知道它是怎么回事!它总是简单的事情,你从来没有尝试过。谢谢