如何在不打开应用程序 Activity 的情况下从后台服务显示对话框 window

how to show dialog window from background service without open application Activity

嘿,我正在开发提醒服务应用程序。我想在应用程序后台服务通知我时显示弹出窗口。在不干扰我的应用程序 activity 或打开 activity 显示弹出窗口的情况下,我可以 cancel/close 弹出窗口。

不幸的是,我尝试了很多方法,但我无法执行此事件以显示与非 Activity class 的对话。现在我的代码没有任何错误。这是我开始查看的地方。它显示等待前景的消息,但我不想打开 activity 并显示弹出窗口。我怎么能做到这一点请帮助我。我在这里发布我的服务代码。

public class StartAlaramService extends Service {

private WindowManager mWindowManager;
private WindowManager.LayoutParams mParams;
private View mRootView;
private Context mContext = null;
private LayoutInflater mInflater = null;
private View alertView = null;


@Override
public void onCreate() {
    super.onCreate();
    mContext=this;
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    try{

        if (null != mWindowManager) {
            if (null != alertView) {
                mWindowManager.removeView(alertView);
            }
            mWindowManager = null;
            mParams = null;
            mInflater = null;
            alertView = null;
        }

    } catch (Exception ex){
        ex.printStackTrace();
        Log.e("SERVICE ", " Code Error");
    }


    showView();
    return StartAlaramService.START_NOT_STICKY;

}

private void initState() {

    Log.e("SERVICE ", "Intiate View State");


    int LAYOUT_FLAG;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    } else {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
    }

        mParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);


    if (null == mWindowManager) {
        mWindowManager = ((WindowManager) mContext.getSystemService(WINDOW_SERVICE));
    }
}

private void showView() {


    try{

        initState();
        // Create the view
        mRootView = initView();
        // Show the view
        mWindowManager.addView(mRootView, mParams);
        // Do some extra stuff when the view's ready


    } catch (Exception ex){
        ex.printStackTrace();
        Log.e("SERVICE ", " EXCEPTION " + ex.getMessage());
    }

}

private View initView() {

        mInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);



        mWindowManager = (WindowManager) mContext.getSystemService(WINDOW_SERVICE);
        LayoutInflater li = LayoutInflater.from(mContext);
        setTheme(R.style.custom_dialog);
        alertView = li.inflate(R.layout.custom_layout_notification, null);


    return alertView;
}

}

SYSTEM ALERT PERMISSION 已在清单中定义。

也许你可以改变类型。

LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

解决SYSTEM ALERT PERMISSION 您必须获得DrawOverlays的许可。您可以通过以下两个步骤获得此权限。

第一步: 将下面的代码放在 Main activity 中,检查是否已授予权限,如果未授予权限,则会请求权限。

if (Build.VERSION.SDK_INT >= 23) {
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
        }
    }

第 2 步: 然后在 onActivityResult.

中添加以下代码后
if (Build.VERSION.SDK_INT >= 23) {
        if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
            if (Settings.canDrawOverlays(this)) {

            }
        }
    }