Android Modal Dialog实现了Singleton模式

时间:2014-09-16 12:14:59

标签: android multithreading dialog modal-dialog singleton

_________________我的单身模式对话课程_________________

⁃   public class CustomDialogMsgOneBtn
⁃   {
⁃   
⁃      private static CustomDialogMsgOneBtn _instance;
⁃   
⁃      private UIHandler uiHandler;
⁃      public Context _context;
⁃      public Dialog _dialog;
⁃      private Object synchObject = new Object();
⁃   
⁃      private TextView _tvDialogTitle;
⁃      private TextView _tvDialogMessage;
⁃      private String _dialogTitle;
⁃      private String _dialogMessage;
⁃      private Button _btnDialogOk;
⁃      private String _btnTextOk;
⁃   
⁃      private CustomDialogMsgOneBtn()
⁃      {
⁃   
⁃      }
⁃      public static CustomDialogMsgOneBtn getInstance()
⁃      {
⁃         if( _instance == null )
⁃         {
⁃            _instance = new CustomDialogMsgOneBtn();
⁃         }
⁃         return _instance;
⁃      }
⁃   
⁃      public void setTitleAndMessage( String title, String msg )
⁃      {
⁃         this._dialogTitle = title;
⁃         this._dialogMessage = msg;
⁃      }
⁃   
⁃      public void initDialog( Context c )
⁃      {
⁃         _context = c;
⁃      }
⁃   
⁃      public void setButtonTextOk( String sText )
⁃      {
⁃         this._btnTextOk = sText;
⁃      }
⁃   
⁃      private void constructUiThread()
⁃      {
⁃         HandlerThread uiThread = new HandlerThread( "UIHandler" );
⁃         uiThread.start();
⁃         uiHandler = new UIHandler( uiThread.getLooper() );
⁃      }
⁃   
⁃      public void show()
⁃      {
⁃         try
⁃         {
⁃            constructUiThread();
⁃   
⁃            uiHandler.sendEmptyMessage( 0 );
⁃   
⁃            synchronized( synchObject )
⁃            {
⁃               try
⁃               {
⁃                  synchObject.wait();
⁃               }
⁃               catch( InterruptedException e )
⁃               {
⁃                  e.printStackTrace();
⁃               }
⁃            }
⁃   
⁃         }
⁃         catch( Exception e )
⁃         {
⁃            e.printStackTrace();
⁃         }
⁃   
⁃      }
⁃   
⁃      public void dismissDialog()
⁃      {
⁃         _dialog.dismiss();
⁃      }
⁃   
⁃      private final class UIHandler extends Handler
⁃      {
⁃   
⁃         public View.OnClickListener btnListener = new View.OnClickListener()
⁃         {
⁃   
⁃            @Override
⁃            public void onClick( View v )
⁃            {
⁃   
⁃               int id = v.getId();
⁃               if( id == R.id.btnDialogOk )
⁃               {
⁃                  Log.d( "dialog Ok", "Ok" );
⁃   
⁃                  notifySync();
⁃               }
⁃   
⁃            }
⁃         };
⁃   
⁃         public DialogInterface.OnDismissListener dialogOnDismissListener = new DialogInterface.OnDismissListener()
⁃         {
⁃   
⁃            @Override
⁃            public void onDismiss( DialogInterface dialog )
⁃            {
⁃   
⁃               notifySync();
⁃               Log.d( "dialog being dismissed", "dismiss ang dialog" );
⁃            }
⁃         };
⁃   
⁃         public UIHandler( Looper looper )
⁃         {
⁃            super( looper );
⁃         }
⁃   
⁃         @Override
⁃         public void handleMessage( Message msg )
⁃         {
⁃            _dialog = new Dialog( _context );
⁃            _dialog.requestWindowFeature( Window.FEATURE_NO_TITLE );
⁃            _dialog.setCanceledOnTouchOutside( true );
⁃            _dialog.setContentView( R.layout.dialog_message_one_button );
⁃            _dialog.setOnDismissListener( dialogOnDismissListener );
⁃   
⁃            _tvDialogTitle = (TextView)_dialog.findViewById( R.id.txtDialogTitle );
⁃            _tvDialogMessage = (TextView)_dialog.findViewById( R.id.txtDialogMsg );
⁃            _btnDialogOk = (Button)_dialog.findViewById( R.id.btnDialogOk );
⁃   
⁃            _tvDialogTitle.setText( _dialogTitle );
⁃            _tvDialogMessage.setText( _dialogMessage );
⁃            _btnDialogOk.setText( _btnTextOk );
⁃   
⁃            _btnDialogOk.setOnClickListener( btnListener );
⁃   
⁃            _dialog.show();
⁃   
⁃         }
⁃   
⁃         private void notifySync()
⁃         {
⁃            synchronized( synchObject )
⁃            {
⁃               _dialog.dismiss();
⁃               synchObject.notifyAll();
⁃            }
⁃         }
⁃   
⁃      }// end UIHanlder class
⁃   
⁃   } //end Class

________________我如何打电话给我的活动类_____________

CustomDialogMsgTwoBtn.getInstance().initDialog(this);
CustomDialogMsgTwoBtn.getInstance().setLeftAndRightButtonText( "cancel", "accept" );
CustomDialogMsgTwoBtn.getInstance().setTitleAndMessage( "Custom Dialog", "test msg" );
CustomDialogMsgTwoBtn.getInstance().show();

09-16 08:41:42.934:I / InputDispatcher(286):应用程序没有响应:AppWindowToken {41c8f4f8 token = Token {41c05558 ActivityRecord {4205b5a0 u0 com.templatea / com.templatea.MainActivity}}} - 窗口{41995bf0 u0 com.example.esf_templatea / com.templatea.MainActivity}。自事件发生以来已经达到5008.4ms,等待起5006.8ms。原因:等待,因为焦点窗口尚未完成处理先前传递给它的输入事件。     09-16 08:41:42.934:I / WindowManager(286):输入事件调度超时发送到com.example / com.templatea.MainActivity     09-16 08:41:43.184:I / Process(286):发送信号。 PID:7296 SIG:3     09-16 08:41:43.194:I / dalvikvm(7296):threadid = 3:对信号3作出反应     09-16 08:41:43.255:I / dalvikvm(7296):将堆栈跟踪写入'/data/anr/traces.txt'     09-16 08:41:43.255:I / Process(286):发送信号。 PID:286 SIG:3     09-16 08:41:43.255:I / dalvikvm(286):threadid = 3:对信号3作出反应     09-16 08:41:43.745:I / dalvikvm(286):将堆栈跟踪写入'/data/anr/traces.txt'     09-16 08:41:43.745:I / Process(286):发送信号。 PID:404 SIG:3     09-16 08:41:43.745:I / dalvikvm(404):threadid = 3:对信号3作出反应     09-16 08:41:43.854:I / dalvikvm(404):将堆栈跟踪写入'/data/anr/traces.txt'     09-16 08:41:43.864:I / Process(286):发送信号。 PID:446 SIG:3     09-16 08:41:43.864:I / dalvikvm(446):threadid = 3:对信号3作出反应     09-16 08:41:43.985:I / dalvikvm(446):将堆栈跟踪写入'/data/anr/traces.txt'     09-16 08:41:44.645:D / dalvikvm(286):GC_EXPLICIT释放3213K,50%免费6742K / 13296K,暂停4ms + 10ms,总计113ms     09-16 08:41:45.234:E / ActivityManager(286):com.emplatea中的ANR(com.templatea / com.templatea.MainActivity)     09-16 08:41:45.234:E / ActivityManager(286):原因:keyDispatchingTimedOut     09-16 08:41:45.234:E / ActivityManager(286):载荷:0.2 / 0.15 / 0.09     09-16 08:41:45.234:E / ActivityManager(286):CPU使用率从17189ms到0ms前:     09-16 08:41:45.234:E / ActivityManager(286):1.1%46 / adbd:0.2%用​​户+ 0.8%内核/故障:180次     09-16 08:41:45.234:E / ActivityManager(286):1.1%286 / system_server:0.9%用户+ 0.1%内核/故障:67次     09-16 08:41:45.234:E / ActivityManager(286):0%404 / com.android.systemui:0%user + 0%kernel     09-16 08:41:45.234:E / ActivityManager(286):0%446 / com.android.phone:0% user + 0%kernel     09-16 08:41:45.234:E / ActivityManager(286):4.9%TOTAL:2.1%用户+ 2.6%内核+ 0.1%softirq     09-16 08:41:45.234:E / ActivityManager(286):CPU使用率从1502ms到2050ms之后:     09-16 08:41:45.234:E / ActivityManager(286):5.5%286 / system_server:3.7%用户+ 1.8%内核/故障:2次     09-16 08:41:45.234:E / ActivityManager(286):9.2%303 / ActivityManager:5.5%用户+ 3.7%内核     09-16 08:41:45.234:E / ActivityManager(286):7.2%TOTAL:7.2%用户+ 0%内核     09-16 08:41:45.375:D / dalvikvm(286):GC_FOR_ALLOC释放1595K,57%免费5837K / 13296K,暂停75ms,总计76ms     09-16 08:41:45.505:D / dalvikvm(286):GC_FOR_ALLOC释放1034K,59%免费5464K / 13296K,暂停72ms,总计73ms     09-16 08:41:45.515:I / Choreographer(286):跳过66帧!应用程序可能在其主线程上做了太多工作。

我的问题是,对于我来说,使用以单例模式实现的模态对话框是完全有效的。它会暂停Activity上的指令指针并继续在Handlter线程下显示Dialog(你可以在singleton类上看到这个)。但我不知道为什么当我按下“后退按钮”它会崩溃我的应用程序并冻结它。希望有人可以帮助我。 :(

2 个答案:

答案 0 :(得分:1)

不要那样做! 一旦停止UI线程,Android系统将向用户显示一个对话框,试图强制关闭!!

使用托管对话框代替http://developer.android.com/guide/topics/ui/dialogs.html 已弃用,但

答案 1 :(得分:0)

我想我不能做我想做的事。这是基于Android的工作原理。

使下面的代码无效

 synchronized( synchObject )
        {
           try
           {
              synchObject.wait();
           }

现在我只使用通信器模式来处理事件和对话框的结果。

非常感谢。