从服务调用的活动

时间:2011-03-14 10:31:57

标签: android

public class oddg extends Activity实现OnClickListener     {         ProgressDialog对话框;         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);

    }

// @Override // public boolean onKeyDown(int keyCode,KeyEvent event){ // if(keyCode == KeyEvent.KEYCODE_BACK&& event.getRepeatCount()== 0){ //
// moveTaskToBack(true); //} //返回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);
        }

    };




}

2 个答案:

答案 0 :(得分: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);



}

尝试这个并告诉我,如果没问题;)

答案 1 :(得分:0)

dialog = new ProgressDialog(this);
代码中的

this指的是onClickListener而不是Activity,请尝试

dialog = new ProgressDialog(arg0)

dialog = new ProgressDialog(oddg.this)

或者你可以实例化 dialog = new ProgressDialog(this);OnCreate()