从Service启动AlertDialog.Builder

时间:2015-05-28 09:14:46

标签: android

我正尝试使用AlertDialog.Builder从服务启动复选框对话框,但我收到以下错误:

启动没有builder.getWindow().setType()的对话框时出现此错误:

05-28 10:48:42.816: E/AndroidRuntime(18510): FATAL EXCEPTION: main
05-28 10:48:42.816: E/AndroidRuntime(18510): Process: com.bustracker, PID: 18510
05-28 10:48:42.816: E/AndroidRuntime(18510): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:691)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.app.Dialog.show(Dialog.java:312)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.app.AlertDialog$Builder.show(AlertDialog.java:991)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at com.bustracker.TrackingService.stop_popup(TrackingService.java:370)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at com.bustracker.TrackingService.onAsyncTaskFinished(TrackingService.java:305)

我试图用builder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

来支持它

但是我收到了这个错误:

  

方法getWindow()未定义类型

private void stop_popup(final ArrayList<Integer> routeList) {


    int routeListSize = routeList.size();

    if (routeListSize > 0) {

        String[] charSequence = new String[routeList.size()];
        for (int i = 0; i < routeList.size(); i++) {
            charSequence[i] = String.valueOf(routeList.get(i));
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("Has this route arrived the stop? ");

        builder.setMultiChoiceItems(charSequence, null,
                new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which,
                            boolean isChecked) {

                        if (isChecked) {

                            route_number = routeList.get(which);

                        }  
                    }
                });

        builder.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                    }
                });
        builder.setNegativeButton(android.R.string.cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                    }
                });

        builder.create();
        builder.show();
    }
}

5 个答案:

答案 0 :(得分:12)

如果您想在Android Service中弹出对话框,则有两种方法:

  1. 使用Activity作为Dialog

  2. 使用AlertDialog.Builder,但您需要将对话框配置为系统警报 使用dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

  3. 以下是示例代码:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Test dialog"));
    builder.setIcon(R.drawable.icon);
    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();
    

    另外,请记住在AndroidManifest.xml中添加权限

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

答案 1 :(得分:1)

试试这个。

创建活动作为对话主题,并从服务启动该活动。

只需要在manifest.xml中注册Activity,如下所示

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

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();
    }
}

答案 2 :(得分:0)

将以下代码添加到stop_popup()方法

AlertDialog alertDialog = builder.create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.show();

并在清单文件中添加权限

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

答案 3 :(得分:0)

清单

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

服务类:HelloService.class

public class HelloService extends Service
{

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Let it continue running until it is stopped.
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return START_STICKY;
}
@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    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();
}
}

来自销毁活动的电话

public class MainActivity extends ActionBarActivity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startService(new Intent(getBaseContext(), HelloService.class));
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings)
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onDestroy()
{
    // TODO Auto-generated method stub
    super.onDestroy();
    stopService(new Intent(getBaseContext(), HelloService.class));
}
}

答案 4 :(得分:0)

val builder = AlertDialog.Builder(this)
builder.setTitle("")
builder.setMessage("")
builder.setPositiveButton("") { dialogInterface, i ->
}
builder.setNegativeButton("1 Year") { dialogInterface, i ->
}
val alert = builder.create()
alert.show()

最好!

Source

相关问题