私は最初のAndroidアプリケーションを作成するプログラマーです。
C#では、通常、達成しようとしていることにThreadWorkerクラスを使用します。
このアプリケーションは、クラス内の変数に基づいて毎秒更新する必要がある一連のテキストボックスで構成されています。更新を行うループを含むバックグラウンドスレッドを開始できるようにしたい。
私が使用しているコードを以下に示します。ループを終了すると更新を1回実行できますが、これを実行しないとGUIが応答しなくなります(ロックアップします)。コードがバックグラウンドで実行されていないと思います。現在、Runnableを使用してこれを試していますが、AsyncTaskまたはExecutorを確認する場合もあります。
package com.example.redkatipo.myfirstapp;
import android.os.*;
import java.util.List;
/**
* Created by Brian on 6/04/2015.
*/
class GroupTimerWorker implements Runnable {
Boolean _stopping = false;
Boolean _stopped = false;
Boolean _paused = false;
List<TimerLine> _lines;
public GroupTimerWorker(List<TimerLine> lines) {
_lines = lines;
}
public void Stop() {
_stopping = true;
}
public void SetPaused(Boolean pause) {
_paused = pause;
}
@Override
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUN D);
while (!_stopping) {
try {
Thread.sleep(1000);
if (!_paused) {
updateAllTimers();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
_stopping = true;
}
}
public synchronized void updateAllTimers() {
for (int i = 0; i < _lines.size(); i++) {
TimerLine tl = _lines.get(i);
if (tl._isRunning) {
tl.SetCurrentDuration();
}
}
}
}
public void updateAllTimers(View view) {
_groupTimerWorker = new GroupTimerWorker(_displayLines);
new Thread (_groupTimerWorker).run();
}
package com.example.redkatipo.myfirstapp;
import android.content.Context;
import android.os.SystemClock;
import android.content.res.Resources;
import android.text.method.BaseKeyListener;
import android.widget.TextView;
import android.widget.Button;
/**
* Created by Brian on 27/03/2015.
*/
パブリッククラスTimerLine {
public Context _context;
public Button _stopStart;
TextView _person;
public TextView _elapsed;
boolean _isRunning;
int _startTime = 0;
int _currentTime = 0;
int _previousTime = 0;
public TimerLine(Context context, Button _control, TextView _id, TextView _output) {
_context = context;
_stopStart = _control;
_person = _id;
_elapsed = _output;
_isRunning = false;
_stopStart.setTag(this);
_elapsed.setText("idle");
}
private int CurrentSeconds()
{
return (int)(SystemClock.uptimeMillis()/1000);
}
public void stopStartButtonClick()
{
if (_isRunning == false) {
_isRunning = true;
// _stopStart.setBackground(_context.getResources().getDrawable(R.drawable.red_button));
_stopStart.setText("Stop");
_elapsed.setBackground(_context.getResources().getDrawable(R.drawable.green_button));
_previousTime = _previousTime + _currentTime - _startTime;
_elapsed.setText(formatTime(_previousTime));
_startTime = CurrentSeconds();
} else {
_isRunning = false;
/// _stopStart.setBackground(_context.getResources().getDrawable(R.drawable.green_button));
_stopStart.setText("Start");
_currentTime = CurrentSeconds();
int difference = (_currentTime - _startTime);
int totalTime = difference + _previousTime;
_elapsed.setText(formatTime(totalTime));
_elapsed.setBackground(_context.getResources().getDrawable(R.drawable.red_button));
}
}
public void SetCurrentDuration()
{
int now = CurrentSeconds();
int difference = now - _startTime;
int totalTime = difference + _previousTime;
_elapsed.setText(formatTime(totalTime));
}
private String formatTime(int totalTime)
{
int minutes = totalTime/60;
int seconds = totalTime % 60;
return "" + minutes + ":" + String.format("%02d", seconds);
}
}
Androidでは、これを行う方法がいくつかあります。しかし、私は以下のプロセスを好みます。
class Yourclass{
Runnable mRunnable ;
int x=1;//number of sec
private void AnyFunction( ) {
mRunnable = new Runnable() {
@Override
public void run() {
<create a handler here say myhandler>
//Optional if you want to start the first call af 'x' sec
myhandler.postDelayed(mRunnable, x * 1000);
}
};
}
private final Handler myhandler= new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case ans1:
{
<your code to change update ui etc>
myhandler.postDelayed(mRunnable, x * 1000);
break;
}
}
/*How to call this from any function wher you need to call the interval if oncreate the on creat call method AnyFunction*/
AnyFunction();
}
これがお役に立てば幸いです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加