2011-10-07 68 views
0

目前我的应用程序从对话框和线程中的服务器获取数据。 来电时应该做些什么。 现在我正在这样做。我想知道这种方法是对的还是别的什么都要做。来电处理线程

@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(); 

    } 
} 

其中t是线程和对话框正在progressDialog。 我也喜欢暂停和恢复是设备进入睡眠状态。

谢谢!

回答

0

它看起来像你真正想做的是为用户创建一个对话框。没有为这里丰富的文档:

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

这里有一个简单的例子:

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

当你想停止对话,呼吁

progressDialog.cancel() 

如果您对Android的线程好奇,我建议看看AsyncTask。这个类(结合Activity.runOnUiThread())应该可以处理Android上99%的线程问题。

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

+0

我想在传入呼叫来临时停止对话框和正在运行的线程并在呼叫结束时重新启动,因为它会返回应用程序。 – voidRy

+0

@ user983364线程无法暂停。打断他们通常是一个坏主意。但是,通常使用标志来模拟这种行为。例如'if(isPaused)Thread.sleep(100)' – spatulamania

+0

thakx for information :)。 – voidRy

0

而不是使用线程使用Asynck任务的其中将相同的Android主题。有四种方法 1. onPreExecute() 2. doInBackground(字符串...为arg0) 3. onProgressUpdate(字符串值...) 4. onPostExecute(虚空结果)

首先启动里面的进度条onPreExecute()然后下载代码在doInBackground()之后,然后在onPostExecute方法内的所有这些关闭进度条之后。

+0

会处理来电或必须完成某些操作来关闭对话并在来电时停止Asyn任务。 – voidRy

+0

是的它也可以处理来电。所有你需要的是在doInBackground(String ... arg0)背景中调用publishProgress(“finish”);它是内置的方法,它会调用内部的onProgressUpdate(String ... values)检查值是否完成,然后关闭进度条。请通过以下链接:http://www.xoriant.com/blog/mobile-application-development/android-async-task.html –

+0

好的!我会试试:) – voidRy