Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends Activity {
- final String LOG_TAG = "myLogs";
- Handler h;
- TextView tvInfo;
- Button btnStart;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tvInfo = (TextView) findViewById(R.id.tvInfo);
- btnStart = (Button) findViewById(R.id.btnStart);
- h = new Handler() {
- public void handleMessage(android.os.Message msg) {
- // обновляем TextView
- tvInfo.setText("Закачано файлов: " + msg.what);
- if (msg.what == 10) btnStart.setEnabled(true);
- };
- };
- }
- public void onclick(View v) {
- switch (v.getId()) {
- case R.id.btnStart:
- btnStart.setEnabled(false);
- Thread t = new Thread(new Runnable() {
- public void run() {
- for (int i = 1; i <= 10; i++) {
- // долгий процесс
- downloadFile();
- h.sendEmptyMessage(i);
- // пишем лог
- Log.d(LOG_TAG, "i = " + i);
- }
- }
- });
- t.start();
- break;
- case R.id.btnTest:
- Log.d(LOG_TAG, "test");
- break;
- default:
- break;
- }
- }
- void downloadFile() {
- // пауза - 1 секунда
- try {
- TimeUnit.SECONDS.sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement