2017-12-28 8 views
0

MainActivityViewPager이 있는데, 어댑터 안에있는 각 이미지를 터치하면 새 Activity이 열립니다.어댑터 클래스에 Interstital Ad을 삽입하는 방법은 무엇입니까?

새로운 활동을로드하기 전에 광고를 게재하는 방법을 아는 사람이 있습니까?

위에서 알 수 있듯이 CustomSwipeAdapterCovers이라는 어댑터가 있으며 새 액티비티를 여는 책임이 있으며 MainActivity에 광고가로드되는 방식을 표시하지만 비 활동에 광고를 구현하는 방법을 모르겠습니다.

public class MainActivity extends AppCompatActivity { 


    private static final int START_LEVEL = 1; 
    private int mLevel; 
    private Button mNextLevelButton; 
    private InterstitialAd mInterstitialAd; 
    private TextView mLevelTextView; 


    private ViewPager viewPager; 
    private CustomSwipeAdapterCapas adapter; 



    //================================= 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Create the next level button, which tries to show an interstitial when clicked. 
     mNextLevelButton = ((Button) findViewById(R.id.next_level_button)); 
     mNextLevelButton.setEnabled(false); 
     mNextLevelButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       showInterstitial(); 
      } 
     }); 



     // Create the text view to show the level number. 
     mLevelTextView = (TextView) findViewById(R.id.level); 
     mLevel = START_LEVEL; 

     // Create the InterstitialAd and set the adUnitId (defined in values/strings.xml). 
     mInterstitialAd = newInterstitialAd(); 
     loadInterstitial(); 

     // Toasts the test ad message on the screen. Remove this after defining your own ad unit ID. 
     Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show(); 



     viewPager = findViewById(R.id.viewpager_main_xml); 
     adapter = new CustomSwipeAdapterCapas(this); 
     viewPager.setAdapter(adapter); 

    } 




    //===================================== 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 


    @Override 
    protected void onStop() { 
     super.onStop(); 

    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 

    } 

    @Override 
    protected void onRestart() { 
     super.onRestart(); 

    } 

    private InterstitialAd newInterstitialAd() { 
     InterstitialAd interstitialAd = new InterstitialAd(this); 
     interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); 
     interstitialAd.setAdListener(new AdListener() { 
      @Override 
      public void onAdLoaded() { 
       mNextLevelButton.setEnabled(true); 
      } 

      @Override 
      public void onAdFailedToLoad(int errorCode) { 
       mNextLevelButton.setEnabled(true); 
      } 

      @Override 
      public void onAdClosed() { 
       // Proceed to the next level. 
       goToNextLevel(); 
      } 
     }); 
     return interstitialAd; 
    } 

    private void showInterstitial() { 
     // Show the ad if it's ready. Otherwise toast and reload the ad. 
     if (mInterstitialAd != null && mInterstitialAd.isLoaded()) { 
      mInterstitialAd.show(); 
     } else { 
      Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show(); 
      goToNextLevel(); 
     } 
    } 

    private void loadInterstitial() { 
     // Disable the next level button and load the ad. 
     mNextLevelButton.setEnabled(false); 
     AdRequest adRequest = new AdRequest.Builder() 
       .setRequestAgent("android_studio:ad_template").build(); 
     mInterstitialAd.loadAd(adRequest); 
    } 

    private void goToNextLevel() { 
     // Show the next level and reload the ad to prepare for the level after. 
     mLevelTextView.setText("Level " + (++mLevel)); 
     mInterstitialAd = newInterstitialAd(); 
     loadInterstitial(); 
    } 

} 

public class CustomSwipeAdapterCovers extends PagerAdapter { 



    private int[] BookCovers = { R.drawable.cover_1, 
            R.drawable.cover_2, 
            R.drawable.cover_3, 
            R.drawable.capa_4, 
            R.drawable.capa_5, 
            R.drawable.capa_6, 
            R.drawable.capa_7, 
            R.drawable.capa_8, 
            R.drawable.capa_9, 
            R.drawable.capa_10, 
            R.drawable.capa_11, 
            R.drawable.capa_12,}; 

    private Context context; 
    private LayoutInflater layoutInflater; 

    public CustomSwipeAdapterCapas(Context context){ 

     this.context = context; 

    } 


    @Override 
    public int getCount() { 
     return capasLivros.length; 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return (view == (LinearLayout) object); 
    } 

    @Override 
    public Object instantiateItem(final ViewGroup container, final int position) { 

     layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View item_view = layoutInflater.inflate(R.layout.capas_layout, container, false); 
     ImageButton imageButton = item_view.findViewById(R.id.imagebutton_main_xml); 
     imageButton.setImageResource(BookCovers[position]); 


     imageButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(position == 0){ 


        Intent intent = new Intent(v.getContext(), LivroUmActivity.class); 
        v.getContext().startActivity(intent); 

       } else if(position == 1){ 


        Intent intent = new Intent(v.getContext(), LivroDoisActivity.class); 
        v.getContext().startActivity(intent); 

       } else if(position == 2){ 

        Intent intent = new Intent(v.getContext(), LivroTresActivity.class); 
        v.getContext().startActivity(intent); 

       } else if(position == 3){ 

        Intent intent = new Intent(v.getContext(), LivroQuatroActivity.class); 
        v.getContext().startActivity(intent); 

       } else if(position == 4){ 

        Intent intent = new Intent(v.getContext(), LivroCincoActivity.class); 
        v.getContext().startActivity(intent); 

       } else if(position == 5){ 

        Intent intent = new Intent(v.getContext(), LivroSeisActivity.class); 
        v.getContext().startActivity(intent); 

       } else if(position == 6){ 

        Intent intent = new Intent(v.getContext(), LivroSeteActivity.class); 
        v.getContext().startActivity(intent); 

       } else if(position == 7){ 

        Intent intent = new Intent(v.getContext(), LivroOitoActivity.class); 
        v.getContext().startActivity(intent); 

       } else if(position == 8){ 

        Intent intent = new Intent(v.getContext(), LivroNoveActivity.class); 
        v.getContext().startActivity(intent); 

       } else if(position == 9){ 

        Intent intent = new Intent(v.getContext(), LivroDezActivity.class); 
        v.getContext().startActivity(intent); 

       } else if(position == 10){ 

        Intent intent = new Intent(v.getContext(), LivroOnzeActivity.class); 
        v.getContext().startActivity(intent); 

       } else if(position == 11){ 

        Intent intent = new Intent(v.getContext(), LivroDozeActivity.class); 
        v.getContext().startActivity(intent); 

       } 

      } 
     }); 


     container.addView(item_view); 
     return item_view; 
    } 

    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     container.removeView((LinearLayout) object); 
     //super.destroyItem(container, position, object); 
    } 

답변

1

또한 바로이 같은 문제에 직면했다. 어댑터 활동에서 삽입 광고를 여는 대신 어댑터 활동의 다음 활동에서 열 수 있습니다. 다음은 나의 해결책입니다. Activity_B를 어댑터 활동으로 고려하십시오.

  • 은 가정하자 3 개 활동가 다음과 같이 개방의 순서는 다음과 같습니다

    Activity_A --> Activity_B --> Activity_C. 
    
  • 가 지금은 Activity_BActivity_C 사이에 삽입 광고를 보여주고 싶어요.

  • 먼저 Activity_A에 삽입 광고를로드 한 다음 Activity_C 번으로 전화 (또는 표시)했습니다.

이 같은 이상 수행 할 수 있습니다 Activity_A 즉 MainActivity에서

는 다음과 같이 코드를 추가 한 OnCreate 내부 Activity_A에서

public void showme(){ 

     mInterstitialAd = new InterstitialAd(this); 
     mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen)); 
     AdRequest adRequest = new AdRequest.Builder() 
       .build(); 
     mInterstitialAd.loadAd(adRequest); 

     mInterstitialAd.setAdListener(new AdListener() 
     { 

      @Override 
      public void onAdClosed() 
      { 
       //reload interstitial 
       AdRequest adRequest = new AdRequest.Builder() 
//      .addTestDevice("YOUR_DEVICE_ID") 
         .build(); 
       mInterstitialAd.loadAd(adRequest); 
      } 
     }); 
    } 

    public static void showInterstitial() { 
     if (mInterstitialAd.isLoaded()) { 
      mInterstitialAd.show(); 
     } 
    } 

콜이 SHOWME를(). Activity_C에서

는 한 OnCreate 내부 코드 아래에 붙여 넣습니다

Activity_A.showInterstitial(); 

또한,이 방법은 어떤 전면 광고의 구글 몹 정책뿐만 아니라 배너 광고를 위반하지 않습니다.

이 솔루션을 구현하는 내 응용 프로그램 PF Balance & Claim Status을 확인하십시오.

이 응용 프로그램에서 주 활동은 어댑터 활동입니다.

+0

답장을 보내 주셔서 감사합니다. 그러나이 오류가 발생합니다. 원인 : java.lang.NullPointerException : 가상 메소드 'boolean com.google.android.gms.ads.InterstitialAd.isLoaded를 호출하려고 시도했습니다.() 'null 객체 참조 – djalmafreestyler

+0

이 코드를 MainActivity에 추가 : https://paste2.org/zwpfLpwv 및 showInterstitial(); 활동 내 : https://paste2.org/VhOX7eAY – djalmafreestyler

관련 문제