2017-08-01 5 views
0

Bonjour, mon problème est que l'émulateur ne montre pas mes annonces de test, voici mon code.

ActivitéAndroid Studio Emulator ne pas afficher Test ADS

package project.chawki.anew.enwproject; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 

public class MainActivity extends AppCompatActivity { 
    private AdView mAdView; 

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

     mAdView = (AdView) findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest.Builder().build(); 
     mAdView.loadAd(adRequest); 
    } 
} 

XML

<com.google.android.gms.ads.AdView 
      xmlns:ads="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/adView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_alignParentBottom="true" 
      ads:adSize="BANNER" 
      ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> 
     </com.google.android.gms.ads.AdView> 

Gradle

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 26 
    buildToolsVersion "26.0.1" 
    defaultConfig { 
     applicationId "project.chawki.anew.enwproject" 
     minSdkVersion 14 
     targetSdkVersion 26 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:26.+' 
    compile 'com.android.support.constraint:constraint-layout:1.0.2' 
    testCompile 'junit:junit:4.12' 
    compile 'com.google.android.gms:play-services-ads:11.0.4' 
} 

Ceci est l'erreur que je suis:

08-01 10:00:53.-3367/com.example.chawkii.myapplication W/Ads: There was a problem getting an ad response. ErrorCode: 0 
08-01 10:00:53.-3210/com.example.chawkii.myapplication W/Ads: Failed to load ad: 0 
08-01 10:01:43.-3210/com.example.chawkii.myapplication E/Ads: JS engine could not be obtained. Cancelling ad request 

developpement d'apprentissage Im et je ne peux pas voir la faute, aussi pour les autorisations dont ils sont bons aussi, mais mes annonces de test ne charge pas dans l'émulateur de périphérique Android. Toute aide s'il vous plaît?

+0

suivre cette voie @chawkii – Jai

+0

https://developers.google.com/android/reference/com/google/android/gms/ads/AdView – Jai

Répondre

1

Après avoir fait beaucoup de recherches, je trouve la solution de cette erreur, le problème, il était pas le code, en fait, il était dans mon émulateur, je devais télécharger la dernière API & également mettre à jour le service google play à la dernière version afin que je puisse voir les annonces

0
public class MyActivity extends Activity { 
    private AdView mAdView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     LinearLayout layout = new LinearLayout(this); 
     layout.setOrientation(LinearLayout.VERTICAL); 

     // Create a banner ad. The ad size and ad unit ID must be set before calling loadAd. 
     mAdView = new AdView(this); 
     mAdView.setAdSize(AdSize.SMART_BANNER); 
     mAdView.setAdUnitId("myAdUnitId"); 

     // Create an ad request. 
     AdRequest.Builder adRequestBuilder = new AdRequest.Builder(); 

     // Optionally populate the ad request builder. 
     adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR); 

     // Add the AdView to the view hierarchy. 
     layout.addView(mAdView); 

     // Start loading the ad. 
     mAdView.loadAd(adRequestBuilder.build()); 

     setContentView(layout); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     // Resume the AdView. 
     mAdView.resume(); 
    } 

    @Override 
    public void onPause() { 
     // Pause the AdView. 
     mAdView.pause(); 

     super.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
     // Destroy the AdView. 
     mAdView.destroy(); 

     super.onDestroy(); 
    } 
} 
+0

ne fonctionne pas pour moi:/ – Chawkii

0

Vous devez ajouter votre émulateur (ou vos autres appareils si vous voulez) comme « dispositif de test ». Les annonces test s'affichent uniquement sur les appareils de test. Pour ce faire, vous devez appeler le addTestDevice lorsque vous créez le AdRequest.

Modifier cette ligne:

AdRequest adRequest = new AdRequest.Builder().build(); 

à:

AdRequest adRequest = new AdRequest.Builder() 
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
    .build(); 
+0

Il Je n'ai pas travaillé pour moi désolé:/ – Chawkii

0
1. Sign into your AdMob account. 

2. Click on Monetize tab. 

3. Select or Create the app and choose the platform. 

4. Select the ad format either Banner or Interstitial and give the ad unit a name. 

5. Once the ad unit is created, you can notice the Ad unit ID on the dashboard. An example of ad unit id look like ca-app-pub-0664570763252260/3326342124 

Create as many ad units required for your app. 
android-admob-creating-ad-unit 

Create two ad units, one for banner and other for interstitial as we need to use the ad unit IDs in the next section. 
3. Creating New Project 

1. Create a new project in Android Studio from File ⇒ New Project. When it prompts you to select the default activity, select Empty Activity and proceed. 

2. Open build.gradle and add play services dependency as AdMob requires it. 

compile ‘com.google.android.gms:play-services-ads:8.4.0’ 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.android.support:design:23.1.1' 

    compile 'com.google.android.gms:play-services-ads:8.4.0' 
} 

3. Add the Ad unit IDs to your strings.xml. Open strings.xml located under res ⇒ values and add the ad units of both Banner and Interstitial. 

<resources> 
    <string name="app_name">AdMob</string> 
    <string name="title_activity_second_activiy">Interstitial</string> 
    <string name="msg_welcome">Welcome to Admob. Click on the below button to launch the Interstitial ad.</string> 
    <string name="btn_fullscreen_ad">Show Fullscreen Ad</string> 

    <!-- AdMob ad unit IDs --> 
    <string name="banner_home_footer">ca-app-pub-0664570763252260/3326522424</string> 
    <string name="interstitial_full_screen">ca-app-pub-0664570763252260/1769900428</string> 

</resources> 

4. Open AndroidManifest.xml and add the below mentioned permissions and other properties. 

> Add INTERNET & ACCESS_NETWORK_STATE permissions. 

> Add google play services version meta-data. 

> Add the AdActivity adding configChanges and theme attributes. 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="info.androidhive.admob"> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <!--Include the AdActivity configChanges and theme. --> 
     <activity 
      android:name="com.google.android.gms.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
      android:theme="@android:style/Theme.Translucent" /> 
    </application> 

</manifest> 

3.1 Adding Banner Ad 

Banner ads occupies only a portion of the screen. I am adding a banner ad in my main activity aligning to bottom of the screen. In order to add the banner ad, you need to add com.google.android.gms.ads.AdView element to your xml layout. 

<com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     ads:adSize="BANNER" 
     ads:adUnitId="@string/banner_home_footer"> 
    </com.google.android.gms.ads.AdView> 

5. Open the layout file of your main activity (activity_main.xml) and add the AdView widget. I am also adding a button to launch another in which we’ll try Interstitial ad. 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="info.androidhive.admob.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/msg_welcome" /> 

    <Button android:id="@+id/btn_fullscreen_ad" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="@string/btn_fullscreen_ad"/> 

    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true" 
     ads:adSize="BANNER" 
     ads:adUnitId="@string/banner_home_footer"> 
    </com.google.android.gms.ads.AdView> 
</RelativeLayout> 

6. Open MainActivity.java and modify the code as shown. 

> Create an instance of AdRequest and load the ad into AdView. 

> Ad the AdView life cycle methods in onResume(), onPause() and in onDestroy() methods. 

package info.androidhive.admob; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

import com.google.android.gms.ads.AdListener; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 

public class MainActivity extends AppCompatActivity { 

    private AdView mAdView; 
    private Button btnFullscreenAd; 

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

     mAdView = (AdView) findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest.Builder() 
       .build(); 
     mAdView.loadAd(adRequest); 

     btnFullscreenAd = (Button) findViewById(R.id.btn_fullscreen_ad); 
     btnFullscreenAd.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, SecondActivity.class)); 
      } 
     }); 
    } 

    @Override 
    public void onPause() { 
     if (mAdView != null) { 
      mAdView.pause(); 
     } 
     super.onPause(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     if (mAdView != null) { 
      mAdView.resume(); 
     } 
    } 

    @Override 
    public void onDestroy() { 
     if (mAdView != null) { 
      mAdView.destroy(); 
     } 
     super.onDestroy(); 
    } 
} 

Now if you run the app, you should see a banner ad at the bottom of your screen. 
android-displaying-admob-banner-ad 
+0

suivre les étapes – Jai

+0

d'abord je voudrais vous remercier pour votre réponse, vous reprenez tout, mais le problème est en fait résolu, ce n'était pas le code, en fait c'était dans mon émulateur, je devais mettre à jour le service Google Play à la dernière version afin que je puisse voir les annonces – Chawkii

0

Vous n'avez pas ajouté d'instruction d'initialisation. Ajouter ceci dans onCreate() {

..... .... MobileAds.initialize (this, "ca-app-pub-3940256099942544 ~ 3347511713");

}