如何从活动传递变量以启动CountDownTimer服务?

时间:2018-03-21 10:05:22

标签: java android android-activity service

这是我的第一个问题,我一直试图找到解决方案几个小时,但无法让它工作。我正在构建一个Android应用程序,它将用户的输入(小时数)转换为快速(不吃)。然后输入到服务,在后台进行倒计时。在此过程中,我希望用户能够访问其他可以从倒数计时器中获得结果的活动(例如,time_left / total_time =完成百分比)。到目前为止,我创建的按钮可用于调用服务。但永远不会调用服务来更新文本视图。感谢

这就是我所拥有的,

public class StartFast extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_fast);
        startService(new Intent(this, MyService.class));
        Log.i("Started service", "hello started service...");
        registerReceiver(br, new IntentFilter("COUNTDOWN_UPDATED"));
    }

    private BroadcastReceiver br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            intent.getExtras();
            long millisUntilFinished = intent.getLongExtra("countdown",0);
            String time = Long.toString((millisUntilFinished));
            TextView tv = findViewById(R.id.timeView1);
            tv.setText(time);
        }
    };

    public void BeginFast(View view){
        //Intent intent = new Intent( this, StartFast.class);
        // below is how to pass an intent for use in a Service to run in the backgroun
       Intent intent =new Intent(this, MyService.class);
       startService(intent);
         //   intent.putExtra() // putExtra longs ...will do after static run succeeds
        //intent.putExtra("data", data); //adding the data

        Intent intent1 = new Intent(this, Heart.class);
        startActivity(intent1);
    }
}

这是服务类,

public class MyService extends Service {

    private final static String TAG = "MyService";
    public static final String COUNTDOWN_BR = "FastBreak.countdown_br";
    Intent bi = new Intent(COUNTDOWN_BR);
    CountDownTimer cdt = null;

    public void OnCreate(){
        super.onCreate();

        Log.i(TAG, "starting timer...");
        cdt = new CountDownTimer(30000,1000) {
            @Override
            public void onTick(long millisUntilFinished){
                Log.i(TAG, "Countdown seconds remaining: " +millisUntilFinished /1000);
                bi.putExtra("countdown", millisUntilFinished);
                sendBroadcast(bi);
            }

            @Override
            public void onFinish(){
                Log.i(TAG, "Timer finished");
            }
        };
        cdt.start();
    }
    @Override
    public void onDestroy() {

        cdt.cancel();
        Log.i(TAG, "Timer cancelled");
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

2 个答案:

答案 0 :(得分:0)

https://github.com/greenrobot/EventBus

检查一下。该库是广播的最佳和最简单的实现。您可以从任何其他对象(在您的情况下为StartFast活动)向任何对象(在您的情况下为StartFast服务)发送任何数据,并编写要运行的代码。

答案 1 :(得分:0)

首先,您需要启动服务并在清单中注册它。服务启动后,它将继续在后台运行。

您可以向服务发送意图,任何已注册听取该意图的广播接收者的人都可以听到。

假设FirstActivity启动了服务,并使用标签BOBBY注册接收器监听意图。该服务是向任何感兴趣并已注册的人发送意图BOBBY的服务。

您想继续使用SecondActivity。在您执行此操作之前,onPause of FirstActivity您需要取消注册该广播接收器。

SecondActivity对标签BOBBY的意图感兴趣,因此他创建了自己的广播接收器并为其注册。

我希望你能看到它的发展方向。广播接收者可以听到你组成的各种意图。

玩得开心。