简体   繁体   中英

how to wake a thread from sleep in android

I have an activity thats waiting for the user press on an image. if the user dont press anything in 3 second i want the activity to close (finish()). thats my code:

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();
}}

My question is how can i wake the Thread if the press was made? i tried Thread.interrupted(); but its not working. the thread still waits 3 seconds if i press or not. Thanks!

Threads are not recommended to use in Android..use Handler to manage time-dependant operations..for example, instead of new Thread() .., try

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

and in onclicklistener:

handler.remove('runnable-that-will-finish-activity') ;
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();

        }
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM