Android创建一个没有按钮的活动
我很困惑,为什么我不能让它工作。我想让一个没有按钮的屏幕在后台做一些事情时运行5秒钟,然后导航回另一个屏幕。这不可能吗 我尝试将代码放在onCreate、onStart和onResume中运行后台项,所有这些方法都是在屏幕实际显示之前启动的。还有别的办法吗 编辑我的代码的最新版本:Android创建一个没有按钮的活动,android,Android,我很困惑,为什么我不能让它工作。我想让一个没有按钮的屏幕在后台做一些事情时运行5秒钟,然后导航回另一个屏幕。这不可能吗 我尝试将代码放在onCreate、onStart和onResume中运行后台项,所有这些方法都是在屏幕实际显示之前启动的。还有别的办法吗 编辑我的代码的最新版本: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sync);
}
protected void onResume(){
super.onResume();
commitSync();
}
private void commitSync(){
TextView monthDayYear = (TextView)findViewById(R.id.month_day_year);
TextView hourMinuteSecond = (TextView)findViewById(R.id.hour_minute_second);
_application = (App)getApplicationContext();
try {
CountDownLatch latch = new CountDownLatch(1);
latch.await(5, TimeUnit.SECONDS);
//Ensure that we have flash
if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
Camera androidCamera = Camera.open();
Camera.Parameters p = androidCamera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
androidCamera.setParameters(p);
//Flash and then turn off
androidCamera.startPreview();
latch.await(50, TimeUnit.MILLISECONDS);
androidCamera.stopPreview();
//Flash screen white
//Get date and time
Module syncModule = _application.getModule();
syncModule.start();
Calendar syncTime = syncModule.getDateAndTime();
//http://stackoverflow.com/questions/21988004/get-time-in-hhmmss-from-seconds-in-java
monthDayYear.setText(new SimpleDateFormat("MM/dd/yyyy").format(syncTime.getTime()));
hourMinuteSecond.setText(new SimpleDateFormat("HH:mm:ss:00").format(syncTime.getTime()));
//Navigate back to MainActivity
Intent mainActivityIntent = new Intent(SyncActivity.this, MainActivity.class);
startActivityForResult(mainActivityIntent, 1);
} else {
throw new Exception("Cannot access Android Flash");
}
} catch (Exception ex) {
Util.appendLog("Exception occured in Sync: " + ex.getMessage());
}
}
也许这对你有帮助:
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run() {
// Do some work like:
finish();
}
}, 500);
这会在延迟一段时间后运行一段代码,您可以将此代码放入所需的屏幕中
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//The code you want to execute
}
}, timeUntilExecution);
这段代码应该在timeUntilExecution变量之后执行run()方法(以毫秒为单位),您可以使用它来执行延迟之后要执行的操作。希望这有帮助。刚刚在编辑中添加了它。commitSync有点复杂,但我希望它在屏幕显示时运行,并且可以无误运行。commitSync()做什么?do?你是在假装加载屏幕吗;为什么不是启动屏幕?commitSync()是在UI线程上运行还是在后台线程上运行?添加了提交同步。它使相机现在闪烁(好吧,至少这是目标)将其添加到onResume()中,解决了这个问题。谢谢