开发者

Handling thread on incoming call

Currently my application fetches data from server inside a dialog box and a thread. What should be done when an开发者_开发百科 incoming call occurs. Right now I'm doing this. I want to know if this methos is right or something else has to done.

    @Override
protected void onPause() {
    // TODO Auto-generated method stub
    Utility.debugger("PAUSE 1");
    if (ScreenReceiver.wasScreenOn) {  

                    // this is the case when onPause() is called by the system due to a screen state change  
                    Utility.killDialog();

                    System.out.println("SCREEN TURNED OFF");  

                } else {  

                    Utility.debugger("PAUSE 2");

                 // this is when onPause() is called when the screen state has not changed  
     }  

    super.onPause();

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    Utility.debugger("PAUSE 3");
    if (!ScreenReceiver.wasScreenOn) {  

               // this is when onResume() is called due to a screen state change  

                     System.out.println("SCREEN TURNED ON"); 

                     Utility.debugger("PAUSE 4");
                        Utility.resumeDialog(); 

                } else {  

                     // this is when onResume() is called when the screen state has not changed



            } 

    super.onResume();


}


public static void killDialog()
{
    if(dialog != null || dialog.isShowing() )
    {dialog.dismiss();
        t.interrupt();
        try {
            t.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public static void resumeDialog()
{
    if(dialog != null  )
    {
        dialog.setIndeterminate(true);
        dialog.show();
        t.run();

    }
}

Where t is thread and dialog is progressDialog. I hv also cheched on pause and resume is the device goes into sleep.

Thanks!


It looks like what you're really trying to do is create a dialog box for the user. There is ample documentation for that here:

http://developer.android.com/guide/topics/ui/dialogs.html

Here's a simple example:

ProgressDialog progressDialog;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.show()

When you want to stop the dialog, call

progressDialog.cancel()

If you are curious about threading on Android, I'd suggest taking a look at AsyncTask. This class (combined with Activity.runOnUiThread()) should handle 99% of threading issues on Android.

http://developer.android.com/reference/android/os/AsyncTask.html


Instead of using Thread use Asynck Task which will same as Thread in android. There are four methods 1. onPreExecute() 2. doInBackground(String... arg0) 3. onProgressUpdate(String...values) 4. onPostExecute(Void result)

First start progress bar inside the onPreExecute() then Downloading code comes under doInBackground() and then after all this dismiss progress bar inside the onPostExecute method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜