cocos2d-js添加百度MSSP插屏(通过jsb反射机制)

1、导入jar包。。。。

2、修改AndroidManifest.xml文件

添加:

<meta-data android:name="BaiduMobAd_APP_ID" android:value="替换为appid"/>
<activity android:configChanges="keyboard|keyboardHidden|orientation" android:name="com.baidu.mobads.AppActivity"/>

添加权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  

3、修改AppActivity.java文件

//百度
import com.baidu.mobads.InterstitialAd;
import com.baidu.mobads.InterstitialAdListener;


// The name of .so is specified in AndroidMenifest.xml. NativityActivity will load it automatically for you.
// You can use "System.loadLibrary()" to load other .so files.

public class AppActivity extends Cocos2dxActivity{
    private static AppActivity app = null;//百度
    public static InterstitialAd interAd;//百度
    static String hostIPAdress="0.0.0.0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        //百度
        app = this;
        String adPlaceId = "代码位id"; // 重要:不填写代码位id不能出广告
        interAd=new InterstitialAd(this, adPlaceId);
        interAd.setListener(new InterstitialAdListener(){
            @Override
            public void onAdClick(InterstitialAd arg0) {
            }
            @Override
            public void onAdDismissed() {
                interAd.loadAd();
            }
            @Override
            public void onAdFailed(String arg0) {
            }
            @Override
            public void onAdPresent() {
            }
            @Override
            public void onAdReady() {
            }
        });
        interAd.loadAd();
        if(nativeIsLandScape()) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        }
        if(nativeIsDebug()){
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
        hostIPAdress = getHostIpAddress();
    }
    public static void sdksMSSP(final String title,final String message) {
        //这里一定要使用runOnUiThread
        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
              if(interAd.isAdReady()){
                    interAd.showAd(app);
                }else{
                    interAd.loadAd();
                }
            }
        });

    }

4、js文件直接调用:

jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "sdksMSSP", "(Ljava/lang/String;Ljava/lang/String;)V", "title", "hahahahha");

 

posted @ 2015-12-18 20:11  recock  阅读(931)  评论(0编辑  收藏  举报