Adnroid Handler的使用

來源:互聯網
上載者:User

這個哥們寫的東西很實際 應用場合很典型,比很多文章嘰歪的都好。

 

Need to notify user when for example user clicks button and in onClick

method program starts some long-time process. Need to have
some solution which will do your task in background.
Handler
is very good solution for this problem.

If you will not use such approach then user will see that program
appears to hang.

How to use it.

First need to to override handleMessage
method.

private Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

// do something

}

};

At second need to create Thread
.

new Thread() {

public void run() {

//long time method

handler.sendEmptyMessage(0);

}

}.start();

I will post whole code for showing whole picture.

Example:

package com.faynasoftlabs.tutorial.android.handlerexample;

import android.app.Activity;

import android.app.ProgressDialog;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

/**

* Class which shows how to work with handler class

* @author FaYna Soft Labs

*/

public class Main extends Activity {

private Button clickBtn;

private ProgressDialog progressDialog;

private Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

progressDialog.dismiss();

}

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

clickBtn = (Button) findViewById(R.id.click);

clickBtn.setText("Click me");

clickBtn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

processThread();

}

});

}

private void processThread() {

progressDialog = ProgressDialog.show(Main.this, "", "Doing...");

new Thread() {

public void run() {

longTimeMethod();

handler.sendEmptyMessage(0);

}

}.start();

}

private void longTimeMethod() {

try {

Thread.sleep(10000);

} catch (InterruptedException e) {

Log.e("tag", e.getMessage());

}

}

}

Using Handler is very useful and easy. If you will follow this
practice your users will not see that your program appears to hang.

But in Android 1.5 and higher versions there is better approach. AsyncTask
class is more preferred. How to use
AsyncTask I will show next time.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.