有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用线程概念在倒计时中运行后台应用程序?

我想在倒计时中做一个后台应用程序,也就是说如果我要开始的话 计时器从那个应用程序中出来,然后转到另一个应用程序 回到同样的倒计时应用程序。我想让计时器 一直跑到我停下来。我知道其中涉及的方法,但我不确定 关于其中使用的线程概念

//MyActivity.class

     import 安卓.app.Activity;
     import 安卓.content.Intent;
     import 安卓.os.Bundle;
     import 安卓.util.Log;
     import 安卓.view.View;
     import 安卓.widget.Button;
     import 安卓.widget.TextView;

     public class MyappActivity extends Activity 
     {
    private static final String Tag = "Background_Timer";
    private Button start;
    private Button stop;
    private TextView tv;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        start = (Button) findViewById(R.id.button);
        stop = (Button) findViewById(R.id.button1);

        tv = (TextView) findViewById(R.id.text1);

        this.runOnUiThread(new Runnable() 
        {
            public void run() 
            {
                tv.setText(MyAppService.seconds + " Seconds Left");
            }
        });

    }

    public void onClick(View src) 
    {
        switch (src.getId()) 
        {
        case R.id.button:
            Log.e(Tag, "onClick: starting service");
            startService(new Intent(this, MyAppService.class));
            break;

        case R.id.button1:
            Log.e(Tag, "onClick: stopping service");
            stopService(new Intent(this, MyAppService.class));
            break;
        }
    }
        }

        //MyService.class

        import 安卓.app.Service;
        import 安卓.content.Intent;
        import 安卓.os.CountDownTimer;
        import 安卓.os.IBinder;
        import 安卓.util.Log;
        import 安卓.widget.TextView;

        public class MyAppService extends Service 
        {

    private static final String TAG = "My Service";
    private static boolean state;
    private static TextView TextTimer;
    public static String seconds;

    MyThread mt = new MyThread();

    @Override
    public void onCreate() 
    {

        CountDownTimer Myapp = new CountDownTimer(50000, 1000) 
        {
            public void onTick(long millisUntilFinished) 
            {
                TextTimer.setText("Seconds left: " + (millisUntilFinished)
                        / 1000);
            }

            public void onFinish() 
            {
                TextTimer.setText("Finished!");
            }
        };  
    }

    public void onStart(Intent intent, int startid) 
    {
        Log.d(TAG, "on-Start");
        mt.start();
    }

    public void onDestroy() 
    {
        Log.d(TAG, "on-Stop");
        mt.stop();
    }

    public static class MyThread extends Thread 
    {

        public void run() 
        {
            try 
            {
                while (state = true) 
                {
                    MyThread.sleep(500);
                }
            } 
            catch (InterruptedException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            };
        }
    }

    @Override
    public IBinder onBind(Intent intent) 
    {
        // TODO Auto-generated method stub
        return null;
    } 
        }

共 (2) 个答案

  1. # 1 楼答案

    首先:不应该在应用程序UI线程中运行倒计时,因为这可能会导致ANRs(应用程序无响应)问题

    可以将SheduledThreadPoolExecutor与自定义Runnable一起使用,也可以使用Bound Service。要回调实际的活动(如果它正在运行),可以使用不同的方法,比如通过Observer Pattern订阅,或者使用LocalBroadcastManager发送意向,然后在自己的线程中更新UI

    正如Lucifer所指出的,您也可以使用TimerTask,但在更新视图时,请确保Runnable调用runOnUiThread

  2. # 2 楼答案

    您可以使用TimerTask类来实现此目的。它与线程相同,但允许您根据时间间隔执行任务。您还可以在其run方法中执行可重复的任务

    请检查简单示例here