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

时间:2019-08-13 05:05:44

标签: android android-service android-alertdialog

嘿,我开发了提醒服务应用程序。当应用程序后台服务通知我时,我想显示弹出窗口。而不干扰我的应用程序活动或打开活动的显示弹出窗口,我可以取消/关闭弹出窗口。

不幸的是,我尝试了许多方法,但无法执行此事件以显示具有非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已在清单中定义。

2 个答案:

答案 0 :(得分:1)

要解决系统警报权限,您必须获得 DrawOverlays 的许可。您可以通过以下两个步骤获得此权限。

第1步:在“主要活动”中放置下面的代码,该代码检查是否授予了许可,否则将请求许可。

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)) {

            }
        }
    }

答案 1 :(得分:0)

也许您可以更改类型。

LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;