2

次のクロノメーターの実装があります。Service内部で使用して、その出力をフラグメントに送信したいと思います。問題は、明らかにServiceまったくないため、ないことです。クロノメーターを内部で動作させる方法はありますか?そうでない場合は、代わりに何を使用できますか?findViewById()ViewService

コード:

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();
4

1 に答える 1

0

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);
}

やなど、何らかの「ステータス」をメイン アクティビティに伝えたい場合は、このメソッドを呼び出し"Time for update"ます"set chrono to " + x + "milli"

于 2014-01-13T12:18:49.487 に答える