2011-03-14 93 views
-1

public class oddg extends Activity实现OnClickListener { ProgressDialog对话框; int increment; int最大值; private static final String TAG =“ServicesDemo”;从服务调用的活动

@Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main1); 
     Button startbtn = (Button) findViewById(R.id.startbtn); 
     startbtn.setOnClickListener(this); 

    } 

// @覆盖 //公共布尔的onkeydown(INT的keyCode,KeyEvent的事件){// 如果(键代码== KeyEvent.KEYCODE_BACK & & event.getRepeatCount()== 0){ //
// moveTaskToBack(true); //} // return super.onKeyDown(keyCode,event); //}

@Override 
    public void onClick(View arg0) 
    { 

     // get the increment value from the text box 
     EditText et = (EditText) findViewById(R.id.increment); 
     // convert the text value to a integer 
     increment = Integer.parseInt(et.getText().toString()); 

     dialog = new ProgressDialog(this); 
     dialog.setCancelable(true); 
     dialog.setMessage("Loading..."); 
     // set the progress to be horizontal 
     dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     // reset the bar to the default value of 0 
     dialog.setProgress(0); 

     // get the maximum value 
     EditText max = (EditText) findViewById(R.id.maximum); 
     // convert the text value to a integer 
      maximum = Integer.parseInt(max.getText().toString()); 
     // set the maximum value 
     dialog.setMax(maximum); 
     // display the progressbar 
     dialog.show(); 

     // create a thread for updating the progress bar 
     Thread background = new Thread (new Runnable() { 
      public void run() 
      { 
       try 
       { 

         // enter the code to be run while displaying the progressbar. 
         // 
         // This example is just going to increment the progress bar: 
         // So keep running until the progress value reaches maximum value 
        while(dialog.getProgress()<= dialog.getMax()) 
        { 
         // wait 500ms between each update 
         Thread.sleep(500); 
         // active the update handler 
         progressHandler.sendMessage(progressHandler.obtainMessage()); 
        } 


       } 
       catch (java.lang.InterruptedException e) 
       { 
        // if something fails do something smart 
       } 
      } 
     }); 
     // start the background thread 
     background.start(); 
     //oddg o1 = new oddg(); 

     // o1.onPause(); 

    } 


    // handler for the background updating 
    Handler progressHandler = new Handler() 
    { 
     public void handleMessage(Message msg) 
     { 
      if(dialog.getProgress()== dialog.getMax()) 
      { 

// Log.d(TAG “的onClick:停止srvice”); //
// stopService(new Intent(oddg.this,MyService.class));

   Log.d(TAG, "onClick: starting service"); 
       startService(new Intent(oddg.this, MyService.class)); 
      } 

      dialog.incrementProgressBy(increment); 
     } 

    }; 




} 
+0

你有什么问题 dialog = new ProgressDialog(this); ? – Nanis 2011-03-14 10:36:08

+0

我已经写了这段代码,但它的nt工作......发生应用程序得到了强制关闭dialog.please告诉我,这是写或wrng? – Smith 2011-03-14 10:38:35

回答

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

@Override 
public void onCreate() { 

Intent i = new Intent(getBaseContext(), CustomDialogExample.class); 
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getApplication().startActivity(i); 



} 

试试这个,说我,如果它的确定;)

+0

hi..its nt working sir .....我是否可以为你提供整个用例????让你知道究竟是什么问题...... !!! – Smith 2011-03-14 10:55:07

+0

是的,如果你可以放更多的代码,尤其是堆栈跟踪会更容易。 – Nanis 2011-03-14 10:58:39

+0

@nanis嗨,其实我已经创建了它包含的应用程序: - 一个布局,我有两个文本框和一个button.as我按下按钮,然后我的进度条开始对话框..我已经打电话到下一个活动(消息对话框)进度条达到最大值对话框消息显示..但我想在后台运行,因为我在玩另一个应用程序,那么我的消息对话框也必须显示在当前的应用程序..... 。现在,对话框消息只会在我的应用程序中弹出。请帮助实现它..... --Thanks提前.... – Smith 2011-03-14 11:01:28

0
dialog = new ProgressDialog(this); 

this在你的代码是指onClickListener不活动,尝试

dialog = new ProgressDialog(arg0) 

dialog = new ProgressDialog(oddg.this)

,或者你可以实例在OnCreate()