Android创建没有按钮的活动

时间:2014-05-30 22:09:59

标签: android

我很困惑为什么我不能让这个工作。我希望有一个屏幕,没有按钮运行5秒,而我在后台做一些事情,然后导航回另一个屏幕。这不可能吗?

我试图将代码放在onCreate,onStart和onResume中运行后台项目,并且在屏幕实际显示之前触发所有这些方法。还有另一种方法吗?

修改我的代码的最新版本:

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

2 个答案:

答案 0 :(得分:2)

也许这有助于你:

Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
    @Override
    public void run() {
        // Do some work like:

        finish();
    }
}, 500);

这会在一段时间后运行代码,您可以将此代码放在所需的屏幕中。

答案 1 :(得分:0)

Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
          @Override
          public void run() {
            //The code you want to execute
          }
        }, timeUntilExecution);

此代码应在timeUntilExecution变量(以毫秒为单位)之后执行run()方法,您可以使用它来执行延迟后要执行的操作。希望这会有所帮助。