如果应用程序被杀,则调用浮动窗口崩溃

时间:2018-06-18 06:50:20

标签: java android android-service android-windowmanager

我想在服务警报或呼叫发生时在屏幕顶部显示浮动布局。通常情况下,当应用程序打开或处于后台时(在刷出之前),它显示并且运行良好。即使我杀了它仍然显示的应用程序(杀死=刷卡应用程序),它仍在工作。但是,如果应用程序被杀死但未打开它会尝试显示(例如来自警报服务)它崩溃,我甚至无法在终止应用程序后看到android工作室中的崩溃日志。 我该如何解决或找到问题?

我的代码Alarm BroadCast,它调用:(根据需要调用警报广播)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleApp1
{
class Program
{
    static void Main(string[] args)
    {
        Screen[] screens = Screen.AllScreens;
    }
}
}

我的浮动视图代码:

public class BroadcastManager extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

    Intent intent = new Intent(context,FloatingWindow.class);
                startService(intent);
}

logcat更新:

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.greenbook.greenlids.MainActivity;
import com.example.greenbook.greenlids.R;

public class FloatingWindow extends Service {

    private WindowManager windowManager;
    private LinearLayout layout;
    private TextView txt;
    private Button bttn1 , exitBttn;
    private Context context;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onCreate() {
        super.onCreate();


        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);


        layout = new LinearLayout(this);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.setBackgroundColor(Color.argb(255,0,0,255));


        layout.setLayoutParams(layoutParams);
        final WindowManager.LayoutParams windowParams;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {

             windowParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
            windowParams.x = 0;
            windowParams.y = 0;
            windowParams.gravity = Gravity.CENTER;

        }else{
            windowParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
            windowParams.x = 0;
            windowParams.y = 0;
            windowParams.gravity = Gravity.CENTER;
        }

        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

        if (layoutInflater != null) {
            layout = (LinearLayout) layoutInflater.inflate(R.layout.linear_layout_floating_window, null);
        }
        layout.setBackgroundColor(Color.argb(255,240,240,255));

        bttn1 = layout.findViewById(R.id.LLbutton);
        exitBttn = layout.findViewById(R.id.exitBttn);
        txt = layout.findViewById(R.id.noteTxt);
        txt.setTextSize(18);
        txt.setText("Natan");
        exitBttn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                windowManager.removeView(layout);
                stopSelf();
            }
        });
        bttn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean handler = new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        txt.setText("Natan The King");
                    }
                }, 1000*5);
            }
        });

        windowManager.addView(layout,windowParams);


        layout.setOnTouchListener(new View.OnTouchListener() {

            private WindowManager.LayoutParams updateParam = windowParams;
            int x , y;
            float touchedX , touchedY;




            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:

                        x = updateParam.x;
                        y = updateParam.y;

                        touchedX = event.getRawX();
                        touchedY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:

                    updateParam.x = (int) (x+event.getRawX() - touchedX);
                    updateParam.y = (int) (y+event.getRawY() - touchedY);

                    windowManager.updateViewLayout(layout,updateParam);
                    break;
                }
                return false;
            }
        });
    } 
}

1 个答案:

答案 0 :(得分:0)

在某些情况下,后台应用会被放置在临时白名单上几分钟。虽然应用程序位于白名单中,但它可以无限制地启动服务,并允许其后台服务运行。当应用程序处理用户可见的任务时,应用程序将放置在白名单中,例如:

处理高优先级的Firebase云消息传递(FCM)消息。 接收广播,例如SMS / MMS消息。 从通知执行PendingIntent。 在VPN应用程序将自身提升到前台之前启动VpnService。 从以下链接中删除 我在这里找到了答案: https://stackoverflow.com/a/46445436/8675712 https://stackoverflow.com/a/47654126/8675712