5つのボタンのクリック後にインタースティシャル広告を表示しようとしましたが、私のコードは機能しません。私は2つの方法を試しましたが、どちらも動作していません。5回のクリックでインタースティシャル広告を表示するにはどうすればよいですか? (共有設定)
PS:私はアンドロイド開発には初めてです。
最初のコード
int counter = 0;
...
\t \t FragmentManager fm = getFragmentManager();
\t \t FragmentTransaction ft = fm.beginTransaction();
\t \t Button play_video = (Button) rootView.findViewById(R.id.play_video);
\t \t play_video.setOnClickListener(new View.OnClickListener() {
\t \t \t @Override
\t \t \t public void onClick(View v) {
\t \t \t \t if (counter == 5) {
\t \t \t \t \t if (mInterstitialAd.isLoaded()) {
\t \t \t \t \t \t mInterstitialAd.show();
\t \t \t \t \t \t counter = 0;
\t \t \t \t \t } else {
\t \t \t \t \t \t counter++;
\t \t \t \t \t \t stream(vidData);
\t \t \t \t \t \t getDialog().dismiss();
\t \t \t \t \t }
\t \t \t \t \t if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
\t \t \t \t \t \t AdRequest adRequest = new AdRequest.Builder().build();
\t \t \t \t \t \t mInterstitialAd.loadAd(adRequest);
\t \t \t \t \t }
\t \t \t \t }
\t \t \t }
\t \t });
第二の符号(共有Preferance)
private int counter = 0;
.....
FragmentManager fm = getFragmentManager();
\t \t FragmentTransaction ft = fm.beginTransaction();
\t \t Button play_video = (Button) rootView.findViewById(R.id.play_video);
\t \t play_video.setOnClickListener(new View.OnClickListener() {
\t \t \t Context context = getActivity();
\t \t \t SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
\t \t \t SharedPreferences.Editor editor = preferences.edit();
\t \t \t int counter = preferences.getInt("CounterR", 0);
\t \t \t @Override
\t \t \t public void onClick(View v) {
\t \t \t \t if (mInterstitialAd.isLoaded() & counter >= 5) {
\t \t \t \t \t counter = 0;
\t \t \t \t \t mInterstitialAd.show();
\t \t \t \t } else {
\t \t \t \t \t counter++;
\t \t \t \t \t stream(vidData);
\t \t \t \t \t getDialog().dismiss();
\t \t \t \t }
\t \t \t \t editor.putInt("CounteR", counter);
\t \t \t \t editor.commit();
\t \t \t \t if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
\t \t \t \t \t AdRequest adRequest = new AdRequest.Builder().build();
\t \t \t \t \t mInterstitialAd.loadAd(adRequest);
\t \t \t \t }
\t \t \t }
\t \t });
更新完全なコード
private int counter = 0;
\t @Override
\t public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
\t \t View rootView = inflater.inflate(R.layout.vid_alert, container, false);
\t \t close_dialog = (Button) findViewById(R.id.close_dialog);
\t \t mInterstitialAd = new InterstitialAd(getActivity());
\t \t mInterstitialAd.setAdUnitId("xxxxxxxxxxxxxxxxxx");
\t \t AdRequest adRequest = new AdRequest.Builder().build();
\t \t // Load ads into Interstitial Ads
\t \t mInterstitialAd.loadAd(adRequest);
\t \t // Create and set AdListener for interstitial
\t \t mInterstitialAd.setAdListener(new AdListener() {
\t \t \t public void onAdClosed() {
\t \t \t \t // When user closes ad end this activity (go back to first
\t \t \t \t // activity)
\t \t \t \t finish();
\t \t \t }
\t \t \t private void finish() {
\t \t \t \t // TODO Auto-generated method stub
\t \t \t }
\t \t });
\t \t getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
\t \t Bundle mArgs = getArguments();
\t \t final String vidData = mArgs.getString("vid_data");
\t \t final String vidID = mArgs.getString("vid_id");
\t \t FragmentManager fm = getFragmentManager();
\t \t FragmentTransaction ft = fm.beginTransaction();
\t \t Context context = getActivity();
\t \t SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
\t \t SharedPreferences.Editor editor = preferences.edit();
\t \t Button stream_video = (Button) rootView.findViewById(R.id.stream_video);
\t \t stream_video.setOnClickListener(new View.OnClickListener() {
\t \t \t @Override
\t \t \t public void onClick(View v) {
\t \t \t \t if (counter == 5) {
\t \t \t \t \t if (mInterstitialAd.isLoaded()) {
\t \t \t \t \t \t mInterstitialAd.show();
\t \t \t \t \t \t AdRequest adRequest = new AdRequest.Builder().build();
\t \t \t \t \t \t mInterstitialAd.loadAd(adRequest);
\t \t \t \t \t \t counter = 0;
\t \t \t \t \t } else {
\t \t \t \t \t \t // means interstitial not yet loaded,so make a load
\t \t \t \t \t \t // request
\t \t \t \t \t \t AdRequest newadRequest = new AdRequest.Builder().build();
\t \t \t \t \t \t mInterstitialAd.loadAd(newadRequest);
\t \t \t \t \t \t // as nothing will happen when we get inside this else
\t \t \t \t \t \t // statement, you should call the stream method from
\t \t \t \t \t \t // here too
\t \t \t \t \t \t streamFB(vidData);
\t \t \t \t \t \t getDialog().dismiss();
\t \t \t \t \t }
\t \t \t \t } else {
\t \t \t \t \t counter++;
\t \t \t \t \t streamFB(vidData);
\t \t \t \t \t getDialog().dismiss();
\t \t \t \t }
\t \t \t }
\t \t });
\t \t Button close_dialog = (Button) rootView.findViewById(R.id.close_dialog);
\t \t close_dialog.setOnClickListener(new View.OnClickListener() {
\t \t \t @Override
\t \t \t public void onClick(View v) {
\t \t \t \t if (counter == 5) {
\t \t \t \t \t if (mInterstitialAd.isLoaded()) {
\t \t \t \t \t \t mInterstitialAd.show();
\t \t \t \t \t \t AdRequest adRequest = new AdRequest.Builder().build();
\t \t \t \t \t \t mInterstitialAd.loadAd(adRequest);
\t \t \t \t \t \t counter = 0;
\t \t \t \t \t } else {
\t \t \t \t \t \t // means interstitial not yet loaded,so make a load
\t \t \t \t \t \t // request
\t \t \t \t \t \t AdRequest newadRequest = new AdRequest.Builder().build();
\t \t \t \t \t \t mInterstitialAd.loadAd(newadRequest);
\t \t \t \t \t \t // as nothing will happen when we get inside this else
\t \t \t \t \t \t // statement, you should call the stream method from
\t \t \t \t \t \t // here too
\t \t \t \t \t \t getDialog().dismiss();
\t \t \t \t \t }
\t \t \t \t } else {
\t \t \t \t \t counter++;
\t \t \t \t \t getDialog().dismiss();
\t \t \t \t }
\t \t \t }
\t \t });
\t \t // Do something else
\t \t return rootView;
ダングリングその他の問題を読み取ります。 –
こんにちは、ありがとう、更新された完全なコードを確認してください、私はラヴィンダーの指示に従っています。 –