Android广告接入平台,Unity接入Android广告: 3、Android端接入小米广告SDK

package com.test.testaar;

import android.app.Activity;

import android.view.Gravity;

import android.view.ViewGroup;

import android.widget.FrameLayout;

import com.miui.zeus.mimo.sdk.MimoSdk;

import com.miui.zeus.mimo.sdk.ad.AdWorkerFactory;

import com.miui.zeus.mimo.sdk.ad.IAdWorker;

import com.miui.zeus.mimo.sdk.listener.MimoAdListener;

import com.xiaomi.ad.common.pojo.AdType;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class AdsTest {

// 请注意,千万要把如下的 APP_ID 替换成您在小米开发者网站上申请的 AppID。不然,可能会影响你的应用广告收益。

private static final String APP_ID = "2882303761517411490";

// 如下两个没有的话就按照如下传入

private static final String APP_KEY = "fake_app_key";

private static final String APP_TOKEN = "fake_app_token";

private Activity _unityActivity;

/**

* 获取unity项目的上下文

* @return

*/

Activity getActivity(){

if(null == _unityActivity) {

try {

Class> classtype = Class.forName("com.unity3d.player.UnityPlayer");

Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);

_unityActivity = activity;

} catch (ClassNotFoundException e) {

} catch (IllegalAccessException e) {

} catch (NoSuchFieldException ignored) {

}

}

return _unityActivity;

}

static boolean callUnity(String gameObjectName, String functionName, String args){

try {

Class> classtype = Class.forName("com.unity3d.player.UnityPlayer");

Method method =classtype.getMethod("UnitySendMessage", String.class,String.class,String.class);

method.invoke(classtype,gameObjectName,functionName,args);

return true;

} catch (ClassNotFoundException e) {

} catch (NoSuchMethodException e) {

} catch (IllegalAccessException e) {

} catch (InvocationTargetException e) {

}

return false;

}

public void Init(){

getActivity().runOnUiThread(new Runnable() {

@Override

public void run() {

callUnity("Main Camera","FromAndroid", " android Init ");

MimoSdk.setDebugOn();

// 正式上线时候务必关闭stage

MimoSdk.setStageOn();

// 如需预置插件请在assets目录下添加mimo_assets.apk

MimoSdk.init(getActivity().getApplicationContext(), APP_ID, APP_KEY, APP_TOKEN);

// 若是api >= 23 须要显式申请权限

// if (Build.VERSION.SDK_INT >= 23) {

// if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED

// || ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED

// || ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {

// ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,

// Manifest.permission.READ_PHONE_STATE, Manifest.permission.INTERNET}, 0);

// }

// }

}

});

}

public void ShowBanner(){

callUnity("Main Camera","FromAndroid", "ShowBanner");

getActivity().runOnUiThread(new Runnable() {

@Override

public void run() {

FrameLayout layout = new FrameLayout(getActivity());

FrameLayout.LayoutParams frameLayout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

frameLayout.gravity=Gravity.BOTTOM|Gravity.CENTER;

getActivity().addContentView(layout, frameLayout);//显示布局管理器

try {

MimoAdListener listener = new MimoAdListener() {

@Override

public void onAdPresent() {

}

@Override

public void onAdClick() {

}

@Override

public void onAdDismissed() {

}

@Override

public void onAdFailed(String s) {

callUnity("Main Camera","FromAndroid", "onAdFailed");

}

@Override

public void onAdLoaded(int i) {

callUnity("Main Camera","FromAndroid", "onAdLoaded");

}

@Override

public void onStimulateSuccess() {

}

};

IAdWorker mBannerAd = AdWorkerFactory.getAdWorker(getActivity(), layout, listener, AdType.AD_BANNER );

mBannerAd.loadAndShow("802e356f1726f9ff39c69308bfd6f06a");

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值