如何在Android中提供视频时自动点击按钮?

时间:2016-07-28 05:33:05

标签: android

我已经在我的应用中集成了一些奖励视频广告,其中包含一个名为Show Video的按钮,当我点击该按钮时导航到视频活动显示视频添加,但问题是视频需要时间来查看,我想显示一个进度对话框,当Videovisibilty变为“true”时,隐藏进度对话框和自动点击按钮以显示视频。

我该怎么做?

mainActivity代码: -

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _button = (Button) findViewById(R.id.show_button);
        _button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, Video.class);
                startActivity(i);
            }
        });


    }

视频活动代码: -

 String TAG = MainActivity.class.getSimpleName();
    private Supersonic mMediationAgent;
    private Placement mPlacement;
    ProgressDialog progress;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        progress = new ProgressDialog(this);
        progress.setMessage("Loading video :) ");
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.setIndeterminate(true);
        progress.show();

        mMediationAgent = SupersonicFactory.getInstance();
        // Supersonic Advertiser SDK call
        SupersonicAdsAdvertiserAgent.getInstance().reportAppStarted(Video.this);
        mMediationAgent.setRewardedVideoListener(Video.this);
        final String userId = "9555515291";
        final String appKey = "50427d";
        mMediationAgent.initRewardedVideo(Video.this, appKey, userId);
        IntegrationHelper.validateIntegration(Video.this);
        if (mMediationAgent.isRewardedVideoAvailable()) {
            mMediationAgent.showRewardedVideo("DefaultRewardedVideo");
        }


    }

    @Override
    public void onRewardedVideoInitSuccess() {
        Log.d(TAG, "onRewardedVideoInitSuccess");
    }

    @Override
    public void onRewardedVideoInitFail(SupersonicError supersonicError) {

    }

    @Override
    public void onRewardedVideoAdOpened() {
        Log.d(TAG, "onRewardedVideoAdOpened");
    }

    @Override
    public void onRewardedVideoAdClosed() {
        // called when the video is closed
        Log.d(TAG, "onRewardedVideoAdClosed");

        // here we show a dialog to the user if he was rewarded
        if (mPlacement != null) {
            // if the user was rewarded
            showRewardDialog(mPlacement);
            mPlacement = null;
        }

    }

    @Override
    public void onVideoAvailabilityChanged(boolean b) {
        // called when the video availbility has changed
        Log.d(TAG, "onVideoAvailabilityChanged" + " " + b);

    }

    @Override
    public void onVideoStart() {
        // called when the video has started
        Log.d(TAG, "onVideoStart");
    }

    @Override
    public void onVideoEnd() {
        // called when the video has ended
        Log.d(TAG, "onVideoEnd");
    }

    @Override
    public void onRewardedVideoAdRewarded(Placement placement) {
        // called when the video has been rewarded and a reward can be given to the user
        Log.d(TAG, "onRewardedVideoAdRewarded" + " " + placement);
        mPlacement = placement;
    }

    @Override
    public void onRewardedVideoShowFail(SupersonicError supersonicError) {
        // called when the video has failed to show
        // you can get the error data by accessing the supersonicError object
        // supersonicError.getErrorCode();
        // supersonicError.getErrorMessage();
        Log.d(TAG, "onRewardedVideoShowFail" + " " + supersonicError);
    }

    public void showRewardDialog(Placement placement) {
        AlertDialog.Builder builder = new AlertDialog.Builder(Video.this);
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        });
        builder.setTitle(getResources().getString(R.string.rewarded_dialog_header));
        builder.setMessage(getResources().getString(R.string.rewarded_dialog_message) + " " + placement.getRewardAmount() + " " + placement.getRewardName());
        builder.setCancelable(false);
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}

1 个答案:

答案 0 :(得分:1)

只需拨打performClick()

即可
 btnViewVideo.performClick();

当你在此时使用它时,onclick listner中的任何动作写入都将自动触发。

欲了解更多信息: fire button's click event programmatically