【发布时间】:2014-10-02 22:12:37
【问题描述】:
我正在尝试使用 Google Play 服务实施 AdMob。 到目前为止,我已经出现了默认的测试横幅,但我想尝试一些测试广告。
我读到模拟器 (AVD) 必须将 Google API 16 或 17 作为目标才能测试 AdMob,但是当我创建一个以此为目标的设备时,模拟器无法加载(我把它留了好久) 20 分钟还没有加载 :( 我只看到闪烁的 android 标志
这是我的 AVD 设备
这是我的 AdFragment 类,其中包含与广告相关的所有代码
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();
}
}
现在我不确定是我的代码没有生成设备 ID 还是我创建的 AVD 设备有问题。我看到的教程是这样的
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("2EAB96D84FE62876379A9C030AA6A0AC")
现在我不知道最后一行是 LogCat 给出的代码还是我只需要输入的代码。我注意到 developer.google 网站有不同的代码,所以我认为我不需要包括在我的代码中,因为我还没有得到它。
请帮忙。谢谢。
更新 1 我在我的主要活动中的 On Resume 中添加了这段代码
@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();
}
}
【问题讨论】: