Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从服务启动AlertDialog.Builder_Android - Fatal编程技术网

Android 从服务启动AlertDialog.Builder

Android 从服务启动AlertDialog.Builder,android,Android,我试图使用AlertDialog.Builder从服务中启动复选框对话框,但出现以下错误: 当我在不使用builder.getWindow().setType()的情况下启动对话框时出现此错误。: 我试图用builder.getWindow().setType(WindowManager.LayoutParams.TYPE\u SYSTEM\u ALERT)对其进行混乱处理 但我有一个错误: 类型的方法getWindow()未定义 private void stop\u弹出窗口(最终ArrayL

我试图使用
AlertDialog.Builder
从服务中启动复选框对话框,但出现以下错误:

当我在不使用
builder.getWindow().setType()的情况下启动对话框时出现此错误。

我试图用
builder.getWindow().setType(WindowManager.LayoutParams.TYPE\u SYSTEM\u ALERT)对其进行混乱处理

但我有一个错误:

类型的方法getWindow()未定义

private void stop\u弹出窗口(最终ArrayList路由列表){
int routeListSize=routeList.size();
如果(routeListSize>0){
String[]charSequence=新字符串[routeList.size()];
对于(int i=0;i
如果你想在Android
服务中弹出一个对话框,你有两种方法:

  • 活动
    用作
    对话框

  • 使用
    AlertDialog.Builder
    ,但需要将对话框配置为系统警报 使用
    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE\系统\警报)

  • 以下是示例代码:

    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" />
    

    将以下代码添加到
    停止弹出()方法中

    AlertDialog alertDialog = builder.create();
            alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            alertDialog.show();
    
    并在清单文件中添加权限

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

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

    只需在manifest.xml中注册您的活动,如下所示

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

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

    <service android:name=".HelloService" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    
    来自销毁活动的呼叫

    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));
    }
    }
    
    最好的


    在menifest中设置权限我以前设置过,但使用getWindow时出现问题,我收到了此错误
    类型的方法getWindow()未定义
    。检查此链接此处是您的问题答案使用
    活动上下文创建AlertDialog。。。它将解决您的问题。谢谢,但您知道为什么对话框窗口的颜色是深灰色而不是普通的浅灰色吗?您可以为警报对话框AlertDialog.Builder=new AlertDialog.Builder设置主题(这是AlertDialog.theme\u HOLO\u light);
    
    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();
        }
    }
    
    <service android:name=".HelloService" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    
    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));
    }
    }
    
    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()