来自 Intent 服务内部的 Android Alert 对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22627184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:15:21  来源:igfitidea点击:

Android Alert dialog from inside an intent service

androidandroid-intentandroid-alertdialogintentservice

提问by Sayed Jalil Hassan

I want to display an alert dialog from inside an intent service.

我想从意图服务内部显示警报对话框。

   AlertDialog alertDialog = new AlertDialog.Builder(this).create();

This throws the following exception

这将引发以下异常

   Unable to add window — token null is not for an application

I have tried IntentService.this and getApplicationContext() as well. Between i dont want to do it using an activity. I just want to show a simple alert dialog with a little text.

我也试过 IntentService.this 和 getApplicationContext() 。我不想使用活动来做到这一点。我只想显示一个带有一点文字的简单警报对话框。

回答by Niranj Patel

Need Activityfor display AlertDialog, because we can't display Dialogfrom any Service

需要Activity显示AlertDialog,因为我们不能Dialog从任何显示Service

Solution.

解决方案。

Create Activityas Dialog Theme and start that Activityfrom Service.

创建Activity为对话框主题并ActivityService.

Just need to register you Activityin menifest.xmllike as below

只需注册你Activitymenifest.xml像如下

android:theme="@android:style/Theme.Dialog"

or

或者

android:theme="@android:style/Theme.Translucent.NoTitleBar"

MyDialog.java

我的对话框

public class MyDialog extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("your title");
        alertDialog.setMessage("your message");
        alertDialog.setIcon(R.drawable.icon);

        alertDialog.show();
    }
}

回答by Eliran Kuta

Only if you set your alertDialog type to TYPE_SYSTEM_ALERTit will be displayed from an intent service.

仅当您将 alertDialog 类型设置为TYPE_SYSTEM_ALERT它时,它才会从意图服务中显示。

 AlertDialog alertDialog = new AlertDialog.Builder(this).create();

add these after your code:

在您的代码后添加这些:

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

But, it have a cost:

但是,它有一个成本:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

回答by Jitesh Upadhyay

Please visit

请拜访

https://github.com/selmantayyar/Custom-SMS-Popup

https://github.com/selmantayyar/Custom-SMS-Popup

it will surly help you!!

它一定会帮助你!!

or what you can do is register anActivity in menifest.xml as follows

或者你可以做的是在menifest.xml中注册一个Activity,如下所示

android:theme="@android:style/Theme.Dialog"

or

或者

android:theme="@android:style/Theme.Translucent.NoTitleBar"

and work around it

并解决它

回答by Jitesh Upadhyay

The problem is because of Context. You can't use thisas Context in Intent Service. So need to pass a Context variable of your Intent Service to your Alert Dialog. Like,

问题是因为Context。您不能将用作意图服务中的上下文。因此需要将您的意图服务的上下文变量传递给您的警报对话框。喜欢,

AlertDialog alertDialog = new AlertDialog.Builder(context).create();