2013年9月10日星期二

[ Novice learning ] activity after the jump run () legacy

private Runnable updateThread = new Runnable () {
int i = 0;

@ Override
public void run () {
Log.d (TAG, "Begin Thread ->" + i);
i = i + 10;
/ / get a message object , Message class is provided by the Android operating system
Message msg = updateBarHandler.obtainMessage () ;
/ / the message object arg1 parameter value is set to i
/ / with arg1 and arg2 two member variables to pass messages , system performance, low consumption
msg.arg1 ; = i;
try {
Thread.sleep (1000);
} catch (InterruptedException e) {
e.printStackTrace () ;/ / print exception information in the command line in the program and the reasons for the position error
}
/ / the message object is added to the message queue
updateBarHandler.sendMessage (msg) ;
if (i == 100) {
/ / If and when i value of 100 when the thread object from the handler to remove
Log. d (TAG, "removeCallbacks updateThread");
updateBarHandler.removeCallbacks (updateThread);
/ / return to main interface
Intent i = new Intent (PBarDisplay.this, MainActivity.class);
PBarDisplay.this.startActivity (i);
PBarDisplay.this.finish ();
}
}
};


learning handler function to write a progress bar progress made ​​after the closure
PBarDisplay.this.startActivity (i);
PBarDisplay.this.finish ();
Jump to the initial activity and close the current activity but spooling Log.d (TAG, "Begin Thread ->" + i);
is why it has been
------ Solution -------------------------- ------------------
if (i == 100)
{
....
} else
{
updateBarHandler.sendMessage (msg);
}

------ For reference only ---------------------------------- -----
actually do not know how to enable that run before closing the thread
------ For reference only ------------------ ---------------------

I look at the code inside your thread does not run while loop , so only run once, you are not through handler to the tune ? Usually by the exiting thread flag to end
There is also the color of how your code into something like a ghost that looks tiring
------ For reference only ---------------- -----------------------

package com.yigedou.app;

import android.util.Log;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;

import com.yigedou.helloworld.MainActivity;
import com.yigedou.helloworld.R;

public class PBarDisplay extends Activity {
private static final String TAG = "PBarDisplay";
private UpdateBarHandler updateBarHandler;
private ProgressBar progressBar;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
// 显示界面布局
setContentView(R.layout.activity_pbar);
// 设置进度条可见
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
progressBar.setVisibility(View.VISIBLE);

// 生成一个HandlerThread对象,实现了使用Looper来处理消息队列的功能,这个类由Android应用程序框架提供
HandlerThread handlerThread = new HandlerThread("handler_thread");
// 在使用HandlerThread的getLooper()方法之前,必须先调用该类的start();
handlerThread.start();
updateBarHandler = new UpdateBarHandler(handlerThread.getLooper());
// 将线程updateThread发送到updateBarHandler中
updateBarHandler.post(updateThread);
}

// 创建自定义的继承与Handler类的子类,其中实现一个参数为Looper对象的构造方法,方法内容调用父类的构造方法即可。
private class UpdateBarHandler extends Handler {
// 以Looper类型参数传递的函数,Looper为消息泵,不断循环的从消息队列中得到消息并处理,因此
// 每个消息队列都有一个Looper,因为Looper是已经封装好了的消息队列和消息循环的类
public UpdateBarHandler(Looper looper) {
// 调用父类的构造函数
super(looper);
}

@Override
public void handleMessage(Message msg) {
progressBar.setProgress(msg.arg1);
updateBarHandler.post(updateThread);
super.handleMessage(msg);
}
}

// 线程类,该类使用匿名内部类的方式进行声明
private Runnable updateThread = new Runnable() {
int i = 0;

@Override
public void run() {
Log.d(TAG, "Begin Thread ->" + i);
i = i + 10;
// 得到一个消息对象,Message类是由Android操作系统提供
Message msg = updateBarHandler.obtainMessage();
// 将message对象的arg1参数的值设置为i
// 用arg1和arg2两个成员变量传递消息,系统性能消耗少
msg.arg1 = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();// 在命令行打印异常信息在程序中出错的位置及原因
}
// 将message对象加入到消息队列中
updateBarHandler.sendMessage(msg);
if (i == 100) {
// 如果当i的值为100时,将线程对象从handler中移除
Log.d(TAG, "removeCallbacks updateThread");
updateBarHandler.removeCallbacks(updateThread);
// 返回主界面
Intent i = new Intent(PBarDisplay.this, MainActivity.class);
PBarDisplay.this.startActivity(i);
PBarDisplay.this.finish();
}
}
};

}



re-posted the code under
------ For reference only ----------------------------- ----------
feeling is done before exiting updateBarHandler.sendMessage (msg); was not and went to run the
handleMessage on the exit operation can be but do not know which directly modify the run there is no good way

In addition handleMessage tips This Handler class should be static or leaks might occur is also a troublesome thing
Baidu point of view that it will be a weak reference also see foggy no prawn help explain
------ For reference only -------- -------------------------------
weak references question
solutions being watched

    private final WeakReference<Activity> mActivity;  
    public MyHandler(Activity activity) {  
        mActivity = new WeakReference<Activity>(activity);  
    }  

problem now is the original I 've got inside

public UpdateBarHandler(Looper looper) {
    // 调用父类的构造函数
    super(looper);
}

Is not there any conflict or should not be such a good way to write ?
------ For reference only -------------------------------------- -

static class UpdateBarHandler extends Handler {
    private final WeakReference<PBarDisplay> mActivity;

    public UpdateBarHandler(PBarDisplay activity) {
      mActivity = new WeakReference<PBarDisplay>(activity);
    }
// 以Looper类型参数传递的函数,Looper为消息泵,不断循环的从消息队列中得到消息并处理,因此
// 每个消息队列都有一个Looper,因为Looper是已经封装好了的消息队列和消息循环的类
public UpdateBarHandler(Looper looper) {
// 调用父类的构造函数
super(looper);
}

@Override
public void handleMessage(Message msg) {
// 从消息队列中得msg的一个成员
PBarDisplay activity = mActivity.get();
activity.setProgress(msg.arg1);
activity.updateBarHandler.post(activity.updateThread);
super.handleMessage(msg);
}
}

没有评论:

发表评论