如何在android中从睡眠中唤醒线程

时间:2012-08-01 09:45:37

标签: android multithreading onclick sleep

我有一个等待用户按下图像的活动。如果用户在3秒内没有按任何东西,我希望活动关闭(完成())。 多数民众赞成我的代码:

private final int delay = 3000;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.after_hangup);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    final ImageView pressToLaunchbrowser = (ImageView) findViewById(R.id.after_hang_up_image);

    pressToLaunchbrowser.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);   // if we want to open the device.
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
            Thread.interrupted();
        }
    }); 
    new Thread() {
        public void run() {
            try {
                    Thread.sleep(delay);
                    finish();
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();
}}

我的问题是如果按下印刷机我怎么能唤醒线程?我试过Thread.interrupted();,但它没有用。如果我按或不按,线程仍会等待3秒。谢谢!

2 个答案:

答案 0 :(得分:3)

建议不要在Android..use Handler中使用线程来管理与时间相关的操作......例如,尝试

而不是new Thread() ..
Handler handler = new Handler();
handler.postDelayed('runnable-that-will-finish-activity', 3000);

并在onclicklistener中:

handler.remove('runnable-that-will-finish-activity') ;

答案 1 :(得分:2)

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;

public class NewActivity extends Activity {

    private final int delay = 3000;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.after_hangup);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        final ImageView pressToLaunchbrowser = (ImageView) findViewById(R.id.after_hang_up_image);

        final Handler handler = new Handler();
        handler.postDelayed(finishRunnable, delay);
        pressToLaunchbrowser.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
                // // if we want to open the device.
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                handler.removeCallbacks(finishRunnable);
            }
        });

    }

    private Runnable finishRunnable = new Runnable() {

        @Override
        public void run() {
            finish();

        }
    };
}