timer thread to refresh the fragment on android

2019-07-26 05:19发布

I have implemented a thread in android which refresh the fragment (some text list ) for every 1 second .

its giving the runtime error while calling the fragment method at thread ,. here is my code

public class RunThreadExtended extends Activity implements Runnable
{
public void run() {

 while(true)
 {  try {
    Thread.sleep(1000);
    AndroidListFragmentActivity.strup++;
    MyListFragment1 fragmentB = (MyListFragment1)getFragmentManager().findFragmentById(R.id.fragment1);
    fragmentB.updatefrag();
} catch (InterruptedException e) {
    e.printStackTrace();
}
  }}}

If I call the fragment method from Mainactivity everything works from , since I need to refresh the thread for every 5 seconds in the backgroud I have implemented like this , but its not working ..pls suggest solution ...

5条回答
叛逆
2楼-- · 2019-07-26 05:43

You could also broadcast an Intent (context.sendBroadcast(intent)) in a thread and receive it in your Activity: Broadcast Receiver

查看更多
时光不老,我们不散
3楼-- · 2019-07-26 05:56

I think you have to do the refreshing in

yourActivity.runOnUiThread(new Runnable() {
public void run()   { /* here */ });

Or via a Handler, or via a post() or in an AsyncTask's onProgress()

查看更多
啃猪蹄的小仙女
4楼-- · 2019-07-26 06:00

You are having errors because you are doing UI operations in a not UI thread. If you change the code into something like this, you will not have that error:

public class RunThreadExtended extends Activity implements Runnable
{
   public void run() {

   while(true)
   {  try {
       Thread.sleep(1000);
       AndroidListFragmentActivity.strup++;
       RunThreadExtended.this.runOnUiThread(new Runnable() { //Use the runOnUIThread method to do your UI hanlding in the UI Thread
           public void run()   {
               MyListFragment1 fragmentB = (MyListFragment1)getFragmentManager().findFragmentById(R.id.fragment1);
               fragmentB.updatefrag();
           }
       });
   } catch (InterruptedException e) {
        e.printStackTrace();
   }
   }}}
查看更多
▲ chillily
5楼-- · 2019-07-26 06:01

You can't update the UI on any thread except for the UI thread. If you want to update the thread you can use a handler that you send a message to every second (handlers handle all messages on the main UI thread).

See

Update UI from Thread

查看更多
再贱就再见
6楼-- · 2019-07-26 06:01

maybe you can do all the work inside a fragment. here are the steps.

  1. define the Handler and Runnable in the fragment.
  2. create the Handler and Runnable in the onCreate() and onAttach()
  3. post the Runnable job in the Handler in onStart().
  4. remove the Runnable job out of the Handler in onStop().

below are the code snippet.

public class JobDetailFragment {

private Handler m_Handler;
private Runnable m_Runnable;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    m_Runnable = new Runnable() {

        @Override
        public void run() {

            updateJobStatus();
        }
    };
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    m_Handler = new Handler();
}

@Override
public void onStart() {
    super.onStart();

    if (m_Handler != null) {
        m_Handler.postDelayed(m_Runnable, PrinterOnUIConstants.PRINT_JOB_AUTO_UPDATE_INTERVAL);
    };
}

@Override
public void onStop() {
    super.onStop();

    // cancel the potential enqueued callback.
    if (m_Handler != null) {
        m_Handler.removeCallbacks(m_Runnable);
    }
}
查看更多
登录 后发表回答