【问题标题】:How to pass a variable from activities to start a CountDownTimer service?如何从活动传递变量以启动 CountDownTimer 服务?
【发布时间】:2018-03-21 10:05:21
【问题描述】:

这是我的第一个问题,我一直在努力寻找解决方案几个小时,但无法让它发挥作用。我正在构建一个 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;
    }
}

【问题讨论】:

  • 您收到广播电话了吗? + 你有没有在menifesst 中声明服务?

标签: java android android-activity service


【解决方案1】:

https://github.com/greenrobot/EventBus

看看这个。这个库是广播的最佳和最简单的实现。您可以从任何其他对象(在您的情况下为 StartFast 活动)将任何数据发送到任何对象(在您的情况下为 StartFast 服务)并编写代码以运行。

【讨论】:

    【解决方案2】:

    首先,您需要启动服务并将其注册到清单中。服务启动后,会一直在后台运行。

    您可以使用服务发送意图,任何注册了广播接收器的人都可以听到该意图。

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

    您想继续进行 SecondActivity。在您这样做之前,您需要在 FirstActivity 的 onPause 上取消注册该广播接收器。

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

    我希望你能看到这是怎么回事。广播接收器可以监听你编造的各种意图。

    玩得开心。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      相关资源
      最近更新 更多