我有一项活动。我想要的是如果用户在一段时间后(由x表示)处于非活动状态,则此活动将自动完成。感谢所有帮助。
我的示例代码:
package com.zilant.testapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class HelpActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_help, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
这是一段活动代码,会在五秒后自动关闭。
public class TestActivity extends ActionBarActivity {
private static final int DELAY = 5000;
public boolean inactive;
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
if(inactive) {
finish();
} else {
mHideHandler.postDelayed(mHideRunnable, DELAY);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_annotation_test);
mHideHandler.postDelayed(mHideRunnable, DELAY);
}
}
现在您需要做的就是在用户交互后将非活动变量更改为true。这将取消活动结束并开始新的倒计时。