开发笔记:在service中弹dialog
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()出现这个错误
最后是看这个朋友的才弄好:http://blog.csdn.net/a740169405/article/details/11720585
调用系统权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
service中的代码:
new Thread(){ public void run() { Looper.prepare(); LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View alert_view=inflater.inflate(R.layout.custom_alert_dialog,null); Button alert_assign= (Button) alert_view.findViewById(R.id.alert_assign); alert_cancel = (Button) alert_view.findViewById(R.id.alert_cancel); TextView alert_message = (TextView) alert_view.findViewById(R.id.alert_message); alert_message.setText(message); countTimer = new CountTimer(15000,1000); final AlertDialog.Builder builder = new AlertDialog.Builder( mContext);// Builder,可以通过此builder设置改变AleartDialog的默认的主题样式及属性相关信息 builder.setView(alert_view); alert_assign.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cancle();// 当取消对话框后进行操作一定的代码?取消对话框 } }); alert_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cancle(); } }); alertDialog = builder.create(); alertDialog.setCancelable(false); alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); alertDialog.show(); countTimer.start(); Looper.loop(); } }.start(); }调用系统的dialog要设置:
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);