问题描述:

I am attempting to implement the AdMob with Google Play Services.

So far I have the default test banner appearing but I want to try some of the test ads.

I read that the emulator (AVD) must have Google APIs 16 or 17 as the target in order to test the AdMob however when I create a device that has this as target the emulator fails to load ( i left it for a good 20 minutes still has not loaded yet :( I just see the flashing android logo

This my AVD device

This is my AdFragment class that contains all the code related to the advertisements

public class AdFragment extends Fragment

{

private AdView mAdView;

@Override

public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {

// TODO Auto-generated method stub

return inflater.inflate(R.layout.fragment_ad, container, false);

}

@Override

public void onActivityCreated(Bundle savedInstanceState)

{

// TODO Auto-generated method stub

super.onActivityCreated(savedInstanceState);

mAdView = (AdView)getView().findViewById(R.id.adView);

AdRequest adRequest = new AdRequest.Builder()

.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)

.build();

// Start loading the ad in the background.

mAdView.loadAd(adRequest);

}

/** Called when leaving the activity */

@Override

public void onPause()

{

if (mAdView != null)

{

mAdView.pause();

}

super.onPause();

}

/** Called when returning to the activity */

@Override

public void onResume()

{

super.onResume();

if (mAdView != null)

{

mAdView.resume();

}

}

/** Called before the activity is destroyed */

@Override

public void onDestroy() {

if (mAdView != null)

{

mAdView.destroy();

}

super.onDestroy();

}

}

Now im unsure whether it is my code that not generating the device ID or a problem with my created AVD device. The tutorials i seen have something like this

.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)

.addTestDevice("2EAB96D84FE62876379A9C030AA6A0AC")

Now i don't know if the last line is the code given by LogCat or is something that i just have to put in. I noticed the developer.google website has a different code so i assume i don't need to include in my code since i have not got it yet.

Please help. thank you.

UPDATE 1

I added this code inside On Resume inside my main activity

@Override

protected void onResume()

{

// TODO Auto-generated method stub

super.onResume();

int isAvaiable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

if(isAvaiable == ConnectionResult.SUCCESS)

{

Log.d("TEST", "GPS IS OK");

}

else if(isAvaiable == ConnectionResult.SERVICE_MISSING || isAvaiable == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED || isAvaiable == ConnectionResult.SERVICE_DISABLED)

{

Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvaiable, this, 1);

dialog.show();

}

}

网友答案:

The API to test banners should be 17 or higher. You have a good answer here that explains it. GPS in emulator.

For the problems related to launch the emulator the only suggestion I can give you is trying VM Acceleration and use an smaller screen. You can try other emulators like x86Emulator and download and the last ISO versions 4.4. In my case the default android emulator took 10-12minutes to be responsive in a ldpi, with the other only 2 in a hdpi.

About the addTestDevice, I think AdRequest.DEVICE_ID_EMULATOR is enough, but if you see in the logcat the MD5 of your device ID then add this hash.

Last but not least, remember to check if GPS are installed at the beginning, it is explained on the DOCS.

To verify the Google Play services version, call isGooglePlayServicesAvailable(). If the result code is SUCCESS, then the Google Play services APK is up-to-date and you can continue to make a connection. If, however, the result code is SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, or SERVICE_DISABLED, then the user needs to install an update.

So you will avoid errors.

相关阅读:
Top