我需要在Android应用中更改为横向模式时永久禁用广告。我列出了代码:
PlayerActivity.java:
package com.grace.view;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.InterstitialAd;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import com.grace.view.ads.Ads;
public class PlayerActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener, AdListener{
// create string variables
String YOUTUBE_APIKEY;
String ID;
private InterstitialAd interstitial;
private static final String LOG_TAG = "LayarTancep";
// create object of view
YouTubePlayerView youTubePlayerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
// connect view object and view id on xml
youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
// create the interstitial
interstitial = new InterstitialAd(this, getString(R.string.admob_id));
// load interstitialAd
Ads.loadInterstitialAd(interstitial);
// set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
// get YOUTUBE APIKEY
YOUTUBE_APIKEY = getString(R.string.youtube_apikey);
// get video id from previous page
Intent i = getIntent();
ID = i.getStringExtra("id");
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
if(result == YouTubeInitializationResult.DEVELOPER_KEY_INVALID)
Toast.makeText(this, "Initialization Fail- key invalid", Toast.LENGTH_SHORT).show();
else if(result == YouTubeInitializationResult.NETWORK_ERROR)
Toast.makeText(this, getString(R.string.no_connection), Toast.LENGTH_SHORT).show();
else if(result == YouTubeInitializationResult.SERVICE_INVALID)
updateYoutubeDialog(
getString(R.string.update_youtube_app),
getString(R.string.update));
else if(result == YouTubeInitializationResult.SERVICE_MISSING)
updateYoutubeDialog(
getString(R.string.no_youtube_app),
getString(R.string.install));
}
void updateYoutubeDialog(String message, String button){
//if Youtube app is not available show alert dialog
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message);
builder.setCancelable(true);
builder.setPositiveButton(button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.youtube"));
startActivity(intent);
//Finish the activity so they can't circumvent the check
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.loadVideo(ID);
}
}
@Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
Log.d(LOG_TAG, "onDismissScreen");
// display youtube player
youTubePlayerView.initialize(YOUTUBE_APIKEY, this);
}
@Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
String message = "onFailedToReceiveAd (" + arg1 + ")";
Log.d(LOG_TAG, message);
// display youtube player
youTubePlayerView.initialize(YOUTUBE_APIKEY, this);
}
@Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
Log.d(LOG_TAG, "onLeaveApplication");
}
@Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
Log.d(LOG_TAG, "onPresentScreen");
}
@Override
public void onReceiveAd(Ad ad) {
// TODO Auto-generated method stub
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
}
答案 0 :(得分:1)
当您想要更改方向配置时,您需要找到为方向更改再次创建的相同活动,在这里您将实现Android生命周期。
所以这个会帮助你
http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/ link