java - 如何在android上的服务中显示对话框

标签 java android android-service android-dialog

在我的应用程序中,我想使用service,并且在此service中我应该显示布局。
我希望当单击 按钮显示对话框时,我编写以下代码!
但是点击按钮后,显示此对话框布局背面,而不显示任何对话框!

我的服务代码:

public class FloatingLayoutService extends Service implements StepperFormListener {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        //Inflate the layout using LayoutInflater
        layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        floatingLayout = (ConstraintLayout) layoutInflater.inflate(R.layout.service_floating_layout, null);

        floatingLay_root = floatingLayout.findViewById(R.id.floatingLay_root);
        floatingLay_main = floatingLayout.findViewById(R.id.floatingLay_main);
        floatingLay_emptyImg = floatingLayout.findViewById(R.id.floatingLay_emptyImg);
        ...

        //Set layout params to display the controls over any screen.
        int LAYOUT_FLAG;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
        }
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                LAYOUT_FLAG,
                0,
                PixelFormat.TRANSLUCENT);
        // From API26, TYPE_PHONE deprecated. Use TYPE_APPLICATION_OVERLAY for O
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
        //Initial position of the floating controls
        params.gravity = Gravity.BOTTOM | Gravity.START;
        params.x = 0;
        params.y = 0;
        //Add the controls view to windowManager
        windowManager.addView(floatingLayout, params);

        //Task page btn
        floatingLay_infoContentNextPage.setOnClickListener(v -> {
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("Title")
                    .setMessage("Are you sure?")
                    .create();

            alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            alertDialog.show();

        });

        return START_STICKY;
    }

如何在布局前面显示对话框

最佳答案

您可以创建一个没有布局的 Activity ,只是为了显示“警报”对话框。

public class MyAlertDialog extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new AlertDialog.Builder(this)
                .setTitle("Title")
                .setMessage("Message")
                ...
                ...
                .show()
    }
}

记住完成 Activity 。

您可以通过 Intent 传递标题和消息来动态设置它们。

服务

Intent intent = new Intent(this, MyAlertDialog.class);
intent.putExtra("titleKey", "title");
intent.putExtra("messageKey", "messge");
startActivity(intent);

在 Activity 中,将其读作:

getIntent().getStringExtra("titleKey")
getIntent().getStringExtra("messageKey")

关于java - 如何在android上的服务中显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60633593/

相关文章:

android - 应用程序终止时从 Activity 调用方法到服务

没有取消注册的Android广播接收器抛出运行时错误

java - 取消部署时,将 EJB 注入(inject) SessionScoped ManagedBean 会生成异常

java - 从 Cloud Firestore 获取数据

java - 如何调用内联类中创建的方法

java - Android NDK 是否为通过套接字发送大 int[] 提供加速?

java - String downloadThumbUri = uploadTask.getResult().getStorage().getDownloadUrl().toString();不适用于上传任务

java - 在Android fragment 中使用键值对填充微调器

android - Android Studio。错误:(19,0)

android - 如何获得与 Service 的更多控制和同步?