如何从图书馆项目中展示admob广告?

时间:2015-12-12 06:37:46

标签: java android admob

这是我的图书馆项目。

public class myads extends Activity implements AdListener {
public static int num;


public static InterstitialAd interstitialAds;
static AdRequest adr;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    interstitialAds = new InterstitialAd(this, "ca-app-pub-*************/**********");
    interstitialAds.setAdListener(this);
    adr=new AdRequest();
    interstitialAds.loadAd(adr);
    interstitialAds.show();

}
public static void intenr(Context c){

interstitialAds.loadAd(adr);
interstitialAds.show();
Toast.makeText(c, "Sample", Toast.LENGTH_LONG).show();
num=3;
 }
@Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub

}
@Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub

}
@Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub

}
@Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub

}
@Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
interstitialAds.show();
}

} 

不幸的是,当我从另一个项目中调用它(这个库项目)时,它停止了错误并且无法正常工作。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    myads.intenr(this);

}

如果我从库项目中删除int.load和int.show,它正在工作并且正在烘烤,已经通过了int。我还正确添加了互联网权限和谷歌活动标签。谢谢你有趣的问题。

1 个答案:

答案 0 :(得分:0)

可能有用 -

private InterstitialAd interstitial;
    private AdRequest adRequestFullScreen;
    private LocalBroadcastManager localBroadcastManager;
    private NotificationReciever notificationReciever;
    private Dialog answerDialog, chatReqDialog, questionDialog;

    private boolean isShowAd = true, isRecieverRegistered = false,
            isNetDialogShowing = false, isGpsDialogShowing = false;
    private String notificationData = "", notificationType;

    // Ad
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId(getString(R.string.admob_fullscreen_ad));

        // live
        // adRequestFullScreen = new AdRequest.Builder().build();

        // test
        adRequestFullScreen = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("").build();

        loadAd();


    public void loadAd() {
        interstitial.loadAd(adRequestFullScreen);
    }

    public void showAd() {
        if (interstitial.isLoaded()) {
            interstitial.show();

        }
    }


        @Override
    public void onDestroy() {
        if (isRecieverRegistered) {
            unregisterReceiver(internetConnectionReciever);
            unregisterReceiver(GpsChangeReceiver);
        }
        AndyUtils.removeSimpleProgressDialog();
        if (isShowAd) {
            showAd();
        }
        Mint.closeSession(getApplicationContext());
        stopLocationUpdate();
        super.onDestroy();
    }