During the Rewarded video play i want to disable the back button and set a minimum waiting time . is it possible to do?On which function should i change to complete my desired functionality?
here is my code
if (mAd.isLoaded()) {
mAd.show();
} else {
startActivity(new Intent(EssayActivityQstnShow2.this, Essay_Answer_Show.class));
}
and here is Rewarded video ads handling methods
private void loadRewardedVideoAds() {
if (!mAd.isLoaded()) {
mAd.loadAd(getResources().getString(R.string.rewardedvideoid), new AdRequest.Builder().build());
}
}
#Override
public void onRewardedVideoAdLoaded() {
}
#Override
public void onRewardedVideoAdOpened() {
// onBackPressed();
}
#Override
public void onRewardedVideoStarted() {
// onBackPressed();
}
#Override
public void onRewardedVideoAdClosed() {
startActivity(new Intent(EssayActivityQstnShow2.this, Essay_Answer_Show.class));
}
#Override
public void onRewarded(RewardItem rewardItem) {
}
#Override
public void onRewardedVideoAdLeftApplication() {
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
loadRewardedVideoAds();
}
#Override
public void onRewardedVideoCompleted() {
startActivity(new Intent(EssayActivityQstnShow2.this, Essay_Answer_Show.class));
}
Try below code
private boolean isVideoPlaying;
#Override
public void onRewardedVideoStarted() {
isVideoPlaying = true;
}
#Override
public void onRewardedVideoAdClosed() {
isVideoPlaying = false;
}
and than onBackPress() check, if the video is playing or not
#Override
public void onBackPressed() {
if (!isVideoPlaying)
super.onBackPressed();
}
Before implementing this, please read the policies carefully.
You must override the onBackPressed callback in activity and remove line super.onBackPressed(). then if the user taps the back button the activity will not close.
If you have no access to the Essay_Answer_Show activity check can you create another activity which extends that or clone library and tries to modify activity?
Related
I am new in android studio, I have this code for showing interstitial.
Now I want to add timer before showing interstitial ad according admob policy.
but no idea how to add this. can someone help me out to do this?
private void goMainScreen(int position) {
if (mInterstitialAd != null) {
mInterstitialAd.show(this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
#Override
public void onAdClicked() {
super.onAdClicked();
}
#Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
startActivity(new Intent(MainActivity.this, SignsActivity.class));
Pref.getInstance(MainActivity.this).setIndex(position);
}
#Override
public void onAdFailedToShowFullScreenContent(#NonNull AdError adError) {
super.onAdFailedToShowFullScreenContent(adError);
startActivity(new Intent(MainActivity.this, SignsActivity.class));
Pref.getInstance(MainActivity.this).setIndex(position);
}
#Override
public void onAdImpression() {
super.onAdImpression();
}
#Override
public void onAdShowedFullScreenContent() {
super.onAdShowedFullScreenContent();
}
});
} else {
startActivity(new Intent(MainActivity.this, SignsActivity.class).putExtra("index", position));
Pref.getInstance(MainActivity.this).setIndex(position);
}
adPosition = adPosition + 1;
}
I am trying to play youtube video, In Picture in Picture mode.
For this I am using youtube player api, when I press back button youtube player activity comes in picture in picture mode but video is stops playing, when i press play button then it again comes in full screen and start playing from start.
In full screen after rotating screen video is playing normally and start playing video where it before rotating.
I am not getting continues playing video in PIP.
Here is my code
public class YoutubePIPActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener
{
private static final String API_KEY = AppConstant.YUTUBE_PLAYER_API_KEY;
private static String mVIDEO_ID = "o7VVHhK9zf0";
private boolean isPIPModeEnabled = true;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube_pip);
YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);
youTubePlayerView.initialize(API_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
{
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(mVIDEO_ID);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult)
{
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onVideoStarted() {
}
};
#Override
public void onBackPressed()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
&& getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
&& isPIPModeEnabled)
{
enterPIPMode();
}
else
{
super.onBackPressed();
}
}
private void enterPIPMode()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
&& getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
Rational rational = new Rational(250, 150);
PictureInPictureParams.Builder params = new PictureInPictureParams.Builder();
params.setAspectRatio(rational);
this.enterPictureInPictureMode(params.build());
} else
{
this.enterPictureInPictureMode();
}
new Handler().postDelayed(() -> checkPIPPermission(), 30);
}
}
#RequiresApi(Build.VERSION_CODES.N)
void checkPIPPermission()
{
isPIPModeEnabled = isInPictureInPictureMode();
if(!isInPictureInPictureMode())
{
onBackPressed();
}
}
#Override
protected void onUserLeaveHint()
{
super.onUserLeaveHint();
enterPIPMode();
}
#Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig)
{
if(newConfig !=null){
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
}
}
}
Here is activity defined in manifest
<activity android:name=".ui.activity.youtube.YoutubePIPActivity"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
android:exported="false"
android:resizeableActivity="true"
android:supportsPictureInPicture="true"
android:theme="#style/Theme.AppCompat.FullScreen"
tools:targetApi="n"/>
I wonder if this is possible to do in AppCompatActivity. I want to let top most Fragment do some action before AppCompatActivity super.onBackPressed(); kill Fragment
HereĀ“s the mockup code which speaks for itself:
#Override
public void onBackPressed() {
ToppMosteFragment.doSomeActionBeforeBackPressedKillYou();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// this is not working..
super.onBackPressed();
}
}, 1000);
}
You can try to make doSomeActionBeforeBackPressedKillYou() return a boolean
and do
#Override
public void onBackPressed() {
if(ToppMosteFragment.doSomeActionBeforeBackPressedKillYou()) {
super.onBackPressed();
}
}
EDIT:
If you defined the animation , you can add a listener :
Animation animation = actionButton.getHideAnimation();
animation.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
listener.onFabAnimationEnd();
}
});
actionButton.setHideAnimation(animation);
listener is a interface that is implement by your activity class
And in you activity
#Override
public void onFabAnimationEnd() {
super.onBackPressed();
}
I got an Idea,
This could be an universal thing -
Create BaseFragment having the method like below -
public class BaseFragment extends Fragment {
public boolean onFragmentBackPressed() {
return true;
}
}
extend your MyFragment to BaseFragment
class MyFragment extends BaseFragment {
// override method
#Override
public boolean onFragmentBackPressed() {
return false;
}
}
Check in your MainActivity that if BaseFragment method onFragmentBackPressed() if returning false or what, if yes - perform some action before exiting
#Override
public void onBackPressed() {
if (MyFragment instance if BaseFragment) {
if (!((BaseFragment) MyFragment).onFragmentBackPressed()) {
// perform and action
} else {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}
I am using this library CamView from LivotovLabs to scan barcodes. It works as expected however it does not focus for specific phones so I want to implement a focus on touch and my solution is as below. However its not working,the phone does not focus on the barcode.
camera = (ScannerLiveView) findViewById(R.id.camview);
camera.setFocusable(true);
camera.setFocusableInTouchMode(true);
camera.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
camera.requestFocus();
return true;
}
});
camera.setScannerViewEventListener(new ScannerLiveView.ScannerViewEventListener() {
#Override
public void onScannerStarted(ScannerLiveView scanner) {
}
#Override
public void onScannerStopped(ScannerLiveView scanner) {
}
#Override
public void onScannerError(Throwable err) {
Toast.makeText(ACME_Scanner.this, "Scanner Error: " + err.getMessage(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCodeScanned(String data) {
Toast.makeText(ACME_Scanner.this, "Barcode No: "+data, Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btnFlash).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggleFlash();
}
});
findViewById(R.id.btnSkip).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
protected void onResume()
{
super.onResume();
ZXDecoder decoder = new ZXDecoder();
decoder.setScanAreaPercent(0.5);
camera.setDecoder(decoder);
camera.startScanner();
}
#Override
protected void onPause()
{
camera.stopScanner();
super.onPause();
}
public void toggleFlash()
{
try {
flashStatus = !flashStatus;
camera.getCamera().getController().switchFlashlight(flashStatus);
}
catch (NullPointerException e){
e.printStackTrace();
}
}
I'm working on a android application and i get mp4 file urls from a json file. Now i want to play these files inside my app using a youtube player. After some research i managed to play my videos within my app. But when i go back from playing videos to other parts of my app,the app seems to be slowed down. I need to know if i'm doing this correctly.
This is my code used to play the video.
if (YouTubeApiServiceUtil.isYouTubeApiServiceAvailable(activity).equals(
YouTubeInitializationResult.SUCCESS)
&& android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Intent intent = YouTubeStandalonePlayer
.createVideoIntent(activity, API_KEY,
video.getFile());
startActivity(intent);
}
from video.getFile(); i'm getting my video url.
i have used YouTubeAndroidPlayerApi.jar as the library.
1.Download YouTubePlyaer API https://developers.google.com/youtube/android/player/downloads/
Register your app on google developer console https://console.developers.google.com
Take an unique API Key and use that in your App.
use below code
public class AboutUs extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_about_us);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(Constants.YOUTUBE_API_KEY, this);
initViews();
}
private void initViews() {
Button btnVisitMega = (Button) findViewById(R.id.btn_visit_megaforties);
Button btnVisitSecurity = (Button) findViewById(R.id.btn_visit_security_seals);
btnVisitMega.setOnClickListener(this);
btnVisitSecurity.setOnClickListener(this);
}
#Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(Constants.YOUTUBE_VIDEO_ID);
}
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onVideoStarted() {
}
};