简体   繁体   English

Android-如何在服务类中显示警报对话框生成器

[英]Android -How to display alert dialog builder in service class

I have Implemented one service class that is (extends Service) in Android .In my service class I have to display one Alert dialog ,How can I do this? 我已经在Android中实现了一个服务类(扩展Service)。在我的服务类中,我必须显示一个Alert对话框,我该怎么办?

Here is my code 这是我的代码

public class BackgroundService extends Service {

@Override
public void onCreate() {
    super.onCreate();
    //Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
    Log.i(tag, "Service created...");

}

@Override
public int  onStartCommand(Intent intent, int flags, int startId) {
    Log.i("LocalService", "Received start id " + startId + ": " + intent);
   AlertDialog.Builder builder = new AlertDialog.Builder(getApplication());
                                builder.setTitle("Test dialog");
                                builder.setMessage("Content");
                                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        //Do something
                                        dialog.dismiss();
                                    }
                                });
                                builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        dialog.dismiss();
                                    }
                                });
                                AlertDialog alert = builder.create();
                                alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                                alert.show();

    return START_STICKY;

}
@Override
public void onDestroy() {
    super.onDestroy();
    t.cancel();
   // Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}

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

In that above code I have Implemented but I don't know how to implement alert dialog in service class 在上面的代码中,我已经实现了,但是我不知道如何在服务类中实现警报对话框

You will have to configure your dialog as System alert as only System alert can be shown using service . 您将必须将对话框配置为“系统警报”,因为只能使用service显示“系统警报”。

Add this to your manifest file : 将其添加到清单文件中:

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

Edit : 编辑:

Create a style for your dialog : 为对话框创建样式:

<style name="dialog" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
</style>

add it while creating the builder : 在创建构建器时添加它:

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getApplication(), R.style.dialog));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM