在活动完全加载后立即自动调用button.performClick()?

时间:2016-05-05 06:57:58

标签: android android-activity

我从后台服务开始活动,该服务解锁手机并开始活动。我想要实现的是在加载活动后立即进入主屏幕。我在解锁后的活动上创建了一个按钮,并使用了我使用的OnClick方法:

private void releaseMediaRecorder(){
    if (mMediaRecorder != null) {
        // clear recorder configuration
        mMediaRecorder.reset();
        // release the recorder object
        mMediaRecorder.release();
        mMediaRecorder = null;
    }
}

现在,我需要在某处调用buttonName.performClick()但在哪里?到目前为止,我尝试过onResume和onPause,但没有运气。如何或在何处确保活动已满载?

moveTaskToBack(true);

这是活动类:

//What I do on my Service to start Activity that unlocks the phone
Intent dialogIntent = new Intent(this, StartStopActivity.class);
                dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(dialogIntent);

2 个答案:

答案 0 :(得分:1)

 private PowerManager mPowerManager;
 private PowerManager.WakeLock mWakeLock;

 public void turnOnScreen(){
     // turn on screen
     Log.v("ProximityActivity", "ON!");
     mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
     mWakeLock.acquire();
}

 @TargetApi(21) //Suppress lint error for PROXIMITY_SCREEN_OFF_WAKE_LOCK
 public void turnOffScreen(){
     // turn off screen
     Log.v("ProximityActivity", "OFF!");
     mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "tag");
     mWakeLock.acquire();
}

使用startActivityForResult和onActivityResult只需打开屏幕并设置参数,以便在需要时保持屏幕开启

答案 1 :(得分:0)

仅在视图上click时才会调用

OnClick 方法。你无法点击任何东西就可以调用它。

只需复制onclick方法中的代码,然后将其放入oncreate().内或您希望执行该代码的任何位置。

如果您实际上没有点击已设置点击监听器的视图,则不会是automatically called

更改您的代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_stop);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
        moveTaskToBack(true);
    }