穿山甲安卓插屏广告


穿山甲安卓插屏广告

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.MainThread;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.bytedance.sdk.openadsdk.TTSplashAd;

import java.util.List;


public class AdService {
private static final String TAG = "AdService";
private static AdService adService = null;

private String mVerticalCodeId = "945020931";
private TTAdNative mTTAdNative;
private TTRewardVideoAd mttRewardVideoAd;
private boolean mHasShowDownloadActive = false;

private String mExpressCodeId = "945052853";
private View mNativeExpressView = null;
private float mNativeExpressViewY = 0;
private float mNativeExpressViewX = 0;
private boolean mNativeExpressViewCanShow = false;

//是否强制跳转到主页面
private boolean mForceGoMain;
//开屏广告加载超时时间,建议大于3000,这里为了冷启动第一次加载到广告并且展示,示例设置了3000ms
private static final int AD_TIME_OUT = 3000;
private String mCodeId = "887299625";
private boolean mIsExpress = false; //是否请求模板广告

public static AdService getInstance(){
if (adService == null){
adService = new AdService();
}
return adService;
}

public void init(AppActivity _activity){
activity = _activity;

TTAdConfig config = new TTAdConfig.Builder()
.appId("5046871")
.useTextureView(true) //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景,可以使用TextureView
.appName("dddd")
.titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)
.allowShowNotify(true) //是否允许sdk展示通知栏提示
.allowShowPageWhenScreenLock(true) //是否在锁屏场景支持展示广告落地页
.debug(false) //测试阶段打开,可以通过日志排查问题,上线时去除该调用
.directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI, TTAdConstant.NETWORK_STATE_3G) //允许直接下载的网络状态集合
.supportMultiProcess(true)//是否支持多进程
.needClearTaskReset()
//.httpStack(new MyOkStack3())//自定义网络库,demo中给出了okhttp3版本的样例,其余请自行开发或者咨询工作人员。
.build();
TTAdSdk.init(_activity.getApplicationContext(), config);
TTAdManager ttAdManager = TTAdSdk.getAdManager();
//step2:(可选,强烈建议在合适的时机调用):申请部分权限,如read_phone_state,防止获取不了imei时候,下载类广告没有填充的问题。
TTAdSdk.getAdManager().requestPermissionIfNecessary(_activity);
//step3:创建TTAdNative对象,用于调用广告请求接口
mTTAdNative = ttAdManager.createAdNative(_activity.getApplicationContext());
loadAd(mVerticalCodeId, TTAdConstant.VERTICAL);
}

/**
* 加载开屏广告
*/
public void loadSplashAd() {
//step3:创建开屏广告请求参数AdSlot,具体参数含义参考文档
AdSlot adSlot = null;
if (mIsExpress) {
//个性化模板广告需要传入期望广告view的宽、高,单位dp,请传入实际需要的大小,
//比如:广告下方拼接logo、适配刘海屏等,需要考虑实际广告大小
float expressViewWidth = UIUtils.getScreenWidthDp(activity);
float expressViewHeight = UIUtils.getHeight(activity);
adSlot = new AdSlot.Builder()
.setCodeId(mCodeId)
.setSupportDeepLink(true)
.setImageAcceptedSize(1080, 1920)
//模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告,请在穿山甲平台查看
.setExpressViewAcceptedSize(expressViewWidth, expressViewHeight)
.build();
} else {
adSlot = new AdSlot.Builder()
.setCodeId(mCodeId)
.setSupportDeepLink(true)
.setImageAcceptedSize(1080, 1920)
.build();
}

//step4:请求广告,调用开屏广告异步请求接口,对请求回调的广告作渲染处理
mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
@Override
@MainThread
public void onError(int code, String message) {
Log.i(TAG, String.valueOf(message));
showToast(message);
goToMainActivity();
}

@Override
@MainThread
public void onTimeout() {
showToast("开屏广告加载超时");
goToMainActivity();
}

@Override
@MainThread
public void onSplashAdLoad(TTSplashAd ad) {
Log.i(TAG, "开屏广告请求成功");
if (ad == null) {
return;
}
//获取SplashView
View view = ad.getSplashView();
Log.i(TAG, "" + activity.isFinishing());
if (view != null && activity.mSplashContainer != null) {
activity.mSplashContainer.removeAllViews();
//把SplashView 添加到ViewGroup中,注意开屏广告view:width >=70%屏幕宽;height >=50%屏幕高
activity.mSplashContainer.addView(view);
//设置不开启开屏广告倒计时功能以及不显示跳过按钮,如果这么设置,您需要自定义倒计时逻辑
//ad.setNotAllowSdkCountdown();
}else {
goToMainActivity();
}

//设置SplashView的交互监听器
ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
@Override
public void onAdClicked(View view, int type) {
Log.i(TAG, "onAdClicked");
showToast("开屏广告点击");
}

@Override
public void onAdShow(View view, int type) {
Log.i(TAG, "onAdShow");
showToast("开屏广告展示");
}

@Override
public void onAdSkip() {
Log.i(TAG, "onAdSkip");
showToast("开屏广告跳过");
goToMainActivity();

}

@Override
public void onAdTimeOver() {
Log.i(TAG, "onAdTimeOver");
showToast("开屏广告倒计时结束");
goToMainActivity();
}
});
if(ad.getInteractionType() == TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
ad.setDownloadListener(new TTAppDownloadListener() {
boolean hasShow = false;

@Override
public void onIdle() {
}

@Override
public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
if (!hasShow) {
showToast("下载中...");
hasShow = true;
}
}

@Override
public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
showToast("下载暂停...");

}

@Override
public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
showToast("下载失败...");

}

@Override
public void onDownloadFinished(long totalBytes, String fileName, String appName) {
showToast("下载完成...");

}

@Override
public void onInstalled(String fileName, String appName) {
showToast("安装完成...");

}
});
}
}
}, AD_TIME_OUT);

}

public void loadExpressAd(int y) {
//mExpressContainer.removeAllViews();

mNativeExpressViewY = y;
mNativeExpressViewX = (1080 - 900) / 2;
if(mNativeExpressView != null) activity.mFrameLayout.removeView(mNativeExpressView);
float expressViewWidth = UIUtils.px2dip(activity,900);
float expressViewHeight = UIUtils.px2dip(activity,800);
//Log.i("!!!!!!!!!!!!!!!!!", "loadExpressAd y:" + y + " x:" + mNativeExpressViewX + " w:" + expressViewWidth + " h:" + expressViewHeight);
//step4:创建广告请求参数AdSlot,具体参数含义参考文档
AdSlot adSlot = new AdSlot.Builder()
.setCodeId(mExpressCodeId) //广告位id
.setSupportDeepLink(true)
.setAdCount(1) //请求广告数量为1到3条
.setExpressViewAcceptedSize(expressViewWidth,expressViewHeight) //期望模板广告view的size,单位dp
.build();
//step5:请求广告,对请求回调的广告作渲染处理
mTTAdNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
@Override
public void onError(int code, String message) {
showToast("load error : " + code + ", " + message);
//mExpressContainer.removeAllViews();
if(mNativeExpressView != null) activity.mFrameLayout.removeView(mNativeExpressView);
}

@Override
public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
if (ads == null || ads.size() == 0){
return;
}
TTNativeExpressAd mTTAd = ads.get(0);
mTTAd.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
@Override
public void onAdClicked(View view, int type) {
showToast("广告被点击");
}

@Override
public void onAdShow(View view, int type) {
showToast("广告展示");
}

@Override
public void onRenderFail(View view, String msg, int code) {
//Log.e("ExpressView","render fail:"+(System.currentTimeMillis() - startTime));
showToast(msg+" code:"+code);
}

@Override
public void onRenderSuccess(View view, float width, float height) {
//Log.e("ExpressView","render suc:"+(System.currentTimeMillis() - startTime));
//返回view的宽高 单位 dp
showToast("渲染成功");
//mExpressContainer.removeAllViews();
if(mNativeExpressView != null) activity.mFrameLayout.removeView(mNativeExpressView);
mNativeExpressView = null;
if(!mNativeExpressViewCanShow) return;
mNativeExpressView = view;
mNativeExpressView.setY(mNativeExpressViewY);
mNativeExpressView.setX(mNativeExpressViewX);
// mNativeExpressView.setBackground(new ColorDrawable(Color.WHITE));
//FrameLayout.LayoutParams linearParams = (FrameLayout.LayoutParams) mNativeExpressView.getLayoutParams();
//linearParams.width = 900;
//linearParams.height = 507;
//mNativeExpressView.setLayoutParams(linearParams);
activity.mFrameLayout.addView(view);
}
});
mTTAd.render();
}
});
}
/**
* 跳转到主页面
*/
private void goToMainActivity() {

activity.mSplashContainer.removeAllViews();
activity.setContentView(activity.mFrameLayout);

loadAd(mVerticalCodeId, TTAdConstant.VERTICAL);
}

private void showToast(String msg) {
Toast.makeText(activity, msg, Toast.LENGTH_LONG);
}

/**
* AT_Banner_Top = 1,//顶上 banner;
* AT_Banner_Bottom,//底部 banner;
* AT_Interstitial,//插屏广告;
* AT_RewardVideo,//视频广告;
* @param type
*/
public void showAd(int type){
Log.i("!!!!!!!!!!!!!!!!!", "showRewardVideoAd showAd type:" + type);
if(type == -1){ // 關閉插屏廣告
if(mNativeExpressView != null) activity.mFrameLayout.removeView(mNativeExpressView);
}else if(type == -2){ // 恢复插屏廣告
if(mNativeExpressView != null) {
activity.mFrameLayout.removeView(mNativeExpressView);
activity.mFrameLayout.addView(mNativeExpressView);
}
}else if(type == -3){ // 彻底關閉插屏廣告
if(mNativeExpressView != null) activity.mFrameLayout.removeView(mNativeExpressView);
mNativeExpressView = null;
mNativeExpressViewCanShow = false;
}else if(type < 0){ // 打開插屏廣告
mNativeExpressViewCanShow = true;
loadExpressAd(-type);
}else{
showRewardVideoAd();
}
}

private void showRewardVideoAd(){

//Log.i("!!!!!!!!!!!!!!!!!", "showRewardVideoAd");
if (mttRewardVideoAd != null) {
//step6:在获取到广告后展示
//该方法直接展示广告
mttRewardVideoAd.showRewardVideoAd(activity);

//展示广告,并传入广告展示的场景
//mttRewardVideoAd.showRewardVideoAd(activity,TTAdConstant.RitScenes.CUSTOMIZE_SCENES,"scenes_test");
mttRewardVideoAd = null;
} else {
loadAd(mVerticalCodeId, TTAdConstant.VERTICAL);
PushJniService.onVideoAdReward(0,4, "1");
}
}
private void loadAd(String codeId, int orientation) {
//Log.i("!!!!!!!!!!!!!!!!!", "loadAd");
//step4:创建广告请求参数AdSlot,具体参数含义参考文档
// AdSlot adSlot = new AdSlot.Builder()
// .setCodeId(codeId)
// .setSupportDeepLink(true)
// .setRewardName("金币") //奖励的名称
// .setRewardAmount(3) //奖励的数量
// .setUserID("user123")//用户id,必传参数
// .setMediaExtra("media_extra") //附加参数,可选
// .setOrientation(orientation) //必填参数,期望视频的播放方向:TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
// .build();
AdSlot adSlot = new AdSlot.Builder()
.setCodeId(codeId)
.setSupportDeepLink(true)
.setAdCount(1)
.setImageAcceptedSize(1080, 1920)
.setRewardName("金币") //奖励的名称
.setRewardAmount(3) //奖励的数量
.setUserID("user123")//用户id,必传参数
.setMediaExtra("media_extra") //附加参数,可选
.setOrientation(orientation) //必填参数,期望视频的播放方向:TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
.build();
//step5:请求广告
mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
@Override
public void onError(int code, String message) {
Log.i("Toast", message);
}

//视频广告加载后,视频资源缓存到本地的回调,在此回调后,播放本地视频,流畅不阻塞。
@Override
public void onRewardVideoCached() {
Log.i("Toast", "rewardVideoAd video cached");
}

//视频广告的素材加载完毕,比如视频url等,在此回调后,可以播放在线视频,网络不好可能出现加载缓冲,影响体验。
@Override
public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
Log.i("Toast", "rewardVideoAd loaded 广告类型:");
mttRewardVideoAd = ad;
mttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {

@Override
public void onAdShow() {
Log.i("Toast", "rewardVideoAd show");
}

@Override
public void onAdVideoBarClick() {
Log.i("Toast", "rewardVideoAd bar click");
}

@Override
public void onAdClose() {
Log.i("Toast", "rewardVideoAd close");
activity.runOnGLThread(new Runnable() {
@Override
public void run() {
PushJniService.onVideoAdReward(1,4, "1");
}
});
}

//视频播放完成回调
@Override
public void onVideoComplete() {
Log.i("Toast", "rewardVideoAd complete");
loadAd(mVerticalCodeId, TTAdConstant.VERTICAL);
}

@Override
public void onVideoError() {
Log.i("Toast", "rewardVideoAd error");
activity.runOnGLThread(new Runnable() {
@Override
public void run() {
PushJniService.onVideoAdReward(0,4, "1");
}
});
}

//视频播放完成后,奖励验证回调,rewardVerify:是否有效,rewardAmount:奖励梳理,rewardName:奖励名称
@Override
public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName) {
Log.i("Toast", "verify:" + rewardVerify + " amount:" + rewardAmount +
" name:" + rewardName);
}

@Override
public void onSkippedVideo() {
Log.i("Toast", "rewardVideoAd has onSkippedVideo");
activity.runOnGLThread(new Runnable() {
@Override
public void run() {
PushJniService.onVideoAdReward(0,4, "1");
}
});
}
});
mttRewardVideoAd.setDownloadListener(new TTAppDownloadListener() {
@Override
public void onIdle() {
mHasShowDownloadActive = false;
}

@Override
public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
if (!mHasShowDownloadActive) {
mHasShowDownloadActive = true;
Log.i("Toast", "下载中,点击下载区域暂停");
}
}

@Override
public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
Log.i("Toast", "下载暂停,点击下载区域继续");
}

@Override
public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
Log.i("Toast", "下载失败,点击下载区域重新下载");
}

@Override
public void onDownloadFinished(long totalBytes, String fileName, String appName) {
Log.i("Toast", "下载完成,点击下载区域重新下载");
}

@Override
public void onInstalled(String fileName, String appName) {
Log.i("Toast", "安装完成,点击下载区域打开");
}
});
}
});
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class UIUtils {

public static float getScreenWidthDp(Context context){
final float scale = context.getResources().getDisplayMetrics().density;
float width = context.getResources().getDisplayMetrics().widthPixels;
return width / (scale <= 0 ? 1 : scale) + 0.5f;
}

//全面屏、刘海屏适配
public static float getHeight(Activity activity) {
hideBottomUIMenu(activity);
float height;
int realHeight = getRealHeight(activity);
if (UIUtils.hasNotchScreen(activity)) {
height = px2dip(activity, realHeight - getStatusBarHeight(activity));
}else {
height = px2dip(activity, realHeight);
}
return height;
}

public static void hideBottomUIMenu(Activity activity) {
if (activity == null) {
return;
}
try {
//隐藏虚拟按键,并且全屏
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = activity.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
//for new api versions.
View decorView = activity.getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
// | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE;
decorView.setSystemUiVisibility(uiOptions);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
} catch (Exception e) {
e.printStackTrace();
}
}

//获取屏幕真实高度,不包含下方虚拟导航栏
public static int getRealHeight(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
display.getRealMetrics(dm);
} else {
display.getMetrics(dm);
}
int realHeight = dm.heightPixels;
return realHeight;
}

//获取状态栏高度
public static float getStatusBarHeight(Context context) {
float height = 0;
int resourceId = context.getApplicationContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
height = context.getApplicationContext().getResources().getDimensionPixelSize(resourceId);
}
return height;
}

public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / (scale <= 0 ? 1 : scale) + 0.5f);
}
/**
* 判断是否是刘海屏
* @return
*/
public static boolean hasNotchScreen(Activity activity){
if (getInt("ro.miui.notch",activity) == 1 || hasNotchAtHuawei(activity) || hasNotchAtOPPO(activity)
|| hasNotchAtVivo(activity) || isAndroidPHasNotch(activity)){ //TODO 各种品牌
return true;
}

return false;
}

/**
* Android P 刘海屏判断
* @param activity
* @return
*/
public static boolean isAndroidPHasNotch(Activity activity){
boolean ret = false;
if (Build.VERSION.SDK_INT >= 28){
try {
Class windowInsets = Class.forName("android.view.WindowInsets");
Method method = windowInsets.getMethod("getDisplayCutout");
Object displayCutout = method.invoke(windowInsets);
if (displayCutout != null) {
ret = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return ret;
}

/**
* 小米刘海屏判断.
* @return 0 if it is not notch ; return 1 means notch
* @throws IllegalArgumentException if the key exceeds 32 characters
*/
public static int getInt(String key,Activity activity) {
int result = 0;
if (isMiui()){
try {
ClassLoader classLoader = activity.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
//参数类型
@SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = int.class;
Method getInt = SystemProperties.getMethod("getInt", paramTypes);
//参数
Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new Integer(0);
result = (Integer) getInt.invoke(SystemProperties, params);

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return result;
}

/**
* 华为刘海屏判断
* @return
*/
public static boolean hasNotchAtHuawei(Context context) {
boolean ret = false;
try {
ClassLoader classLoader = context.getClassLoader();
Class HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");
Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
ret = (boolean) get.invoke(HwNotchSizeUtil);
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (Exception e) {
} finally {
return ret;
}
}

public static final int VIVO_NOTCH = 0x00000020;//是否有刘海
public static final int VIVO_FILLET = 0x00000008;//是否有圆角

/**
* VIVO刘海屏判断
* @return
*/
public static boolean hasNotchAtVivo(Context context) {
boolean ret = false;
try {
ClassLoader classLoader = context.getClassLoader();
Class FtFeature = classLoader.loadClass("android.util.FtFeature");
Method method = FtFeature.getMethod("isFeatureSupport", int.class);
ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (Exception e) {
} finally {
return ret;
}
}
/**
* OPPO刘海屏判断
* @return
*/
public static boolean hasNotchAtOPPO(Context context) {
return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
}

public static boolean isMiui() {
boolean sIsMiui = false;
try {
Class<?> clz = Class.forName("miui.os.Build");
if (clz != null) {
sIsMiui = true;
//noinspection ConstantConditions
return sIsMiui;
}
} catch (Exception e) {
// ignore
}
return sIsMiui;
}
}