如果没有注册触摸,请在10秒后切换到主要活动

时间:2014-12-08 10:53:42

标签: android

我正在Android平板电脑上开发调查应用程序,我想做到这一点。当一个活动运行,如果你没有触摸屏幕直到10秒我想自动离开主要活动我该怎么办?我使用下面的代码但是我收到了错误。

static int x = 0; // must be static

Bundle get_data;
int bas_i;
TextView txt, text2;

// Button secretbtn;

String Type;
int TypeID;
int GrpID;
int id;

int sorusayisi = 0;
int cevaplanan = 0;
boolean hata = false;

int akis_soru_sorusayisi = 0;

String last_Type_inmethod;

Bundle data_gonder;

// Dynamic Buttons

Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;

private static Handler handler;
private Runnable runnable;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sonraki_olay);

    handler.postDelayed(runnable, 10*1000);

    btn1 = new Button(getApplicationContext());
    btn2 = new Button(getApplicationContext());
    btn3 = new Button(getApplicationContext());
    btn4 = new Button(getApplicationContext());
    btn5 = new Button(getApplicationContext());

    txt = (TextView) findViewById(R.id.textView1);

    // secretbtn = (Button)findViewById(R.id.secret_button);
    //
    // secretbtn.setOnClickListener(this);
    // secretbtn.setVisibility(View.VISIBLE);
    // secretbtn.setBackgroundColor(Color.TRANSPARENT);

    get_data = getIntent().getExtras();
    bas_i = get_data.getInt("data_sonraki_soru_id");
    // txt.setText(""+i);
    // Log.i("get_data_from_sonraki_Activity", ""+i);

    data_gonder = new Bundle();

    gonder();

}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_UP) {
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable, 10 * 1000);
    }
    return super.dispatchTouchEvent(ev);
}

public void run() {
    // start activity here
    Intent i = getBaseContext().getPackageManager()
            .getLaunchIntentForPackage(getBaseContext().getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
}

3 个答案:

答案 0 :(得分:0)

您不应该从后台线程启动活动。您可以使用处理程序来实现:

更改以下代码:

    Intent i = getBaseContext().getPackageManager()
            .getLaunchIntentForPackage(
                    getBaseContext().getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    startActivity(i);

为:

new Handler(Looper.getMainLooper()).post(
     new Runnable(){
           Intent i = getBaseContext().getPackageManager()
                    .getLaunchIntentForPackage(
                            getBaseContext().getPackageName());
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            startActivity(i);
     }
);

答案 1 :(得分:0)

您可以通过执行以下操作来完成此操作 -

在您的活动中创建一个新字段以存储延迟:

private long delay = 0;

onCreate

    //Check for touches on our main layout
    LinearLayout ll_main = (LinearLayout) findViewById(R.id.survey_main_layout);
    ll_main.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //If someone touches, reset our delay
            delay = 0;
            return false;
        }
    };

如果您想开始检查延迟,请填写以下内容:

    //Start a thread that will keep count of the time
    new Thread("Listen for touch thread") {
        public void run() {
            //Keep checking until the activity dies
            while(!this.isFinishing()) {
                try{
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //Incement the delay
                delay += 50;

                //If our delay in MS is over 10,000
                if (delay >= 10000) {
                    //Create a new intent on our UI Thread and start it!
                    //Change myActivity to the name of your activity!
                    myActivity.this.runOnUiThread(new Runnable() {
                       public void run() {
                           Intent i = getBaseContext().getPackageManager()
                                .getLaunchIntentForPackage(
                                        getBaseContext().getPackageName());
                            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                            startActivity(i);
                    });
                    return; //Return to end the thread
                }
            }
        }
    }.start();

如果你想在每次回到这个活动时想要这样做,那么一个明智的想法是

new Thread() { ... }.start();

代码进入 onResume 方法,并重置延迟。

答案 2 :(得分:0)

你得到的错误可能是因为你已经停止了UI线程太长时间,Android不喜欢它并向用户发送错误,因为你的应用现在没有响应(这正是UI线程存在的原因) )。

正确的方法是使用Handler和postDelayed方法:

public class MyActivity extends Activity {
    private Handler mHandler = new Handler();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mHandler.postDelayed(new Runnable() {
                public void run() {
                    startActivity();
                }
            }, 10000);
        }

        private void startActivity() {
            Intent i = getBaseContext().getPackageManager()
                .getLaunchIntentForPackage(
                        getBaseContext().getPackageName());
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
        }
    }

此外,为了做你想做的事,你应该设置一个取消处理程序的onClick。