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();
}
}
});
}
}