内部服务天文台表 [英] Chronometer inside Service

查看:171
本文介绍了内部服务天文台表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的天文台表的实现。我想用它服务里然后送到其输出到我的片段。问题是,服务没有 findViewById(),因为很明显它没有查看可言。有没有什么办法可以让天文台表里面服务工作,如果不是我可以用呢?

code:

 天文台计时表=(天文台)findViewById(R.id.chronometer);
    chronometer.setBase(SystemClock.elapsedRealtime());    chronometer.setOnChronometerTickListener(
            新Chronometer.OnChronometerTickListener(){                @覆盖
                公共无效onChronometerTick(天文台时计){
                    // TODO自动生成方法存根
                    长myElapsedMillis = SystemClock.elapsedRealtime() - chronometer.getBase();
                    字符串strElapsedMillis =将String.valueOf(myElapsedMillis);                    // Toast.makeText(AndroidChronometer.this,strElapsedMillis,Toast.LENGTH_SHORT).show();
                    TextView的TW5 =(的TextView)findViewById(R.id.textView2);
                    tw5.setText(strElapsedMillis.format(%d个最小数:%d秒,
                            TimeUnit.MILLISECONDS.toMinutes(myElapsedMillis)
                            TimeUnit.MILLISECONDS.toSec​​onds(myElapsedMillis) -
                                    TimeUnit.MINUTES.toSec​​onds(TimeUnit.MILLISECONDS.toMinutes(myElapsedMillis))));
                }
            }
    );
    chronometer.start();


解决方案

一种选择就是使用

  postDelayed(Runnable的R,长毫秒)

触发任何你更新周期,不使用你的服务,可运行的更新方法。然后在您的更新方法更新UI。

另一种选择是使用的AsyncTask,而不是服务,如果你的目的是做一些事情的背景。 AsyncTask的访问在其 onProgressUpdate()法的用户界面,并可以有做你的东西。

如果你必须使用一个服务,那么你必须从服务到用户界面活动,广播,让UI活动做的观点更新:

这在您的主要活动:

 私人StatusReceiver mReceiver;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    ...    mReceiver =新StatusReceiver();    ...
}
@覆盖
保护无效调用onStart(){
    //注册的广播接收机
    IntentFilter的过滤器=新的IntentFilter();
    filter.addAction(的getString(R.string.ACTION_UPDATE_CHRONO));
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    registerReceiver(mReceiver,过滤器);    ...
}@覆盖
保护无效的onStop(){
    ...
    unregisterReceiver(mReceiver);
    ...
}公共类StatusReceiver扩展广播接收器{    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        字符串行动= intent.getAction();
        如果(action.equals(的getString(R.string.ACTION_UPDATE_CHRONO))){
             //做你的UI的东西在这里
        }
    }
}

在服务类:

 私人无效广播(字符串状态,异常错误){
    意图broadcastIntent =新意图((意向)的getText(R.string.ACTION_UPDATE_CHRONO));
    broadcastIntent.putExtra(STATUS,地位);
    broadcastIntent.putExtra(ERROR,错误);
    sendBroadcast(broadcastIntent);
}

当你想传达一些身份到你的主要活动,如时间更新设置计时调用此方法+ X +毫

I have a following chronometer implementation. I'd like to use it inside Service then sent its output to my fragment. The problem is that Service has no findViewById() because obviously it has no View at all. Is there any way I can get the chronometer to work inside Service and if not what can I use instead?

code:

Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);
    chronometer.setBase(SystemClock.elapsedRealtime());

    chronometer.setOnChronometerTickListener(
            new Chronometer.OnChronometerTickListener() {

                @Override
                public void onChronometerTick(Chronometer chronometer) {
                    // TODO Auto-generated method stub
                    long myElapsedMillis = SystemClock.elapsedRealtime() - chronometer.getBase();
                    String strElapsedMillis = String.valueOf(myElapsedMillis);

                    //  Toast.makeText(AndroidChronometer.this, strElapsedMillis, Toast.LENGTH_SHORT).show();
                    TextView tw5 = (TextView) findViewById(R.id.textView2);
                    tw5.setText(strElapsedMillis.format("%d min : %d sec",
                            TimeUnit.MILLISECONDS.toMinutes(myElapsedMillis),
                            TimeUnit.MILLISECONDS.toSeconds(myElapsedMillis) -
                                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(myElapsedMillis))));
                }
            }
    );


    chronometer.start();

解决方案

One option is just to use

postDelayed(Runnable r, long milliseconds)

to trigger your runnable update method for whatever you update period is, without using a service. And then update the UI in your update method.

Another option is to use AsyncTask instead of Service, if your intention is to do something in the background. AsyncTask has access to the UI on its onProgressUpdate() method and you can do your stuff there.

If you have to use a Service, then you have to broadcast from the service to the UI activity and let the UI Activity do the view update:

This in your main activity:

private StatusReceiver mReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...

    mReceiver = new StatusReceiver();

    ...
}


@Override
protected void onStart() {
    // Register the broadcast receiver
    IntentFilter filter = new IntentFilter();
    filter.addAction(getString(R.string.ACTION_UPDATE_CHRONO));
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    registerReceiver(mReceiver, filter);

    ...
}

@Override
protected void onStop() {
    ...
    unregisterReceiver(mReceiver);
    ...
}

public class StatusReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if( action.equals(getString(R.string.ACTION_UPDATE_CHRONO)) ) {
             // Do your UI stuff here
        } 
    }
}

In the service class:

private void broadcast(String status, Exception error) {
    Intent broadcastIntent = new Intent((Intent) getText(R.string.ACTION_UPDATE_CHRONO));
    broadcastIntent.putExtra("STATUS", status);
    broadcastIntent.putExtra("ERROR", error);
    sendBroadcast(broadcastIntent);
}

Call this method when you want to communicate some "status" to your main activity, like "Time for update" or "set chrono to " + x + "milli".

这篇关于内部服务天文台表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆