概述:
由于第三方检测机构的要求,公司app需要在root环境中进行弹框提示。
步骤一:
package com.example.xy.service;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import com.example.xy.utils.RootUtil;
public class DetectRootService extends Service {
private Runnable runnable;
private Handler handler;
public DetectRootService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
private void getData() {
new Thread(new Runnable() {
@Override
public void run() {
try{
//检查当前环境是否为root环境,如果为root环境
RootUtil.isRoot = RootUtil.isDeviceRooted();
}catch(Exception e){
e.printStackTrace();
}
}
}).start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("服务启动测试","启动");
getData();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.e("服务关闭测试","关闭");
super.onDestroy();
}
}
步骤二:
在application类中的onCreate方法中添加,activity生命周期管理
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(@NonNull Activity activity) {
//开启
Intent intent1 = new Intent(activity, DetectRootService.class);
startService(intent1);
}
@Override
public void onActivityResumed(@NonNull Activity activity) {
if (RootUtil.isRoot) {
//检查RootUtil的状态,如果当前为root环境,弹出弹框警告
CustomDialog.build().setMaskColor(Color.parseColor("#4D000000"))
.setCustomView(new OnBindView<CustomDialog>(R.layout.dialog_address_delete) {
@Override
public void onBind(CustomDialog dialog, View v) {
TextView tv_content = v.findViewById(R.id.tv_content);
TextView tv_delete = v.findViewById(R.id.stv_delete);
TextView tv_cancel = v.findViewById(R.id.stv_cancel);
TextView title = v.findViewById(R.id.title);
tv_delete.setText("确定");
tv_content.setText("App在不安全环境中运行");
title.setText("警告");
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setClickable(false);
v.setEnabled(false);
dialog.dismiss();
}
});
tv_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setClickable(false);
v.setEnabled(false);
dialog.dismiss();
}
});
}
}).setAlign(CustomDialog.ALIGN.CENTER).show();
}
}
@Override
public void onActivityPaused(@NonNull Activity activity) {
}
@Override
public void onActivityStopped(@NonNull Activity activity) {
}
@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
}
@Override
public void onActivityDestroyed(@NonNull Activity activity) {
//关闭服务
Intent intent1 = new Intent(activity, DetectRootService.class);
stopService(intent1);
}
});
root环境检测工具类
package com.example.xy.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
public class RootUtil {
public static boolean isRoot;
public static boolean isDeviceRooted() {
return checkRootMethod1() || checkRootMethod2() || checkRootMethod3(); //如果不想使用该功能给以直接返回false
}
private static boolean checkRootMethod1() {
String buildTags = android.os.Build.TAGS;
return buildTags != null && buildTags.contains("test-keys");
}
private static boolean checkRootMethod2() {
String[] paths = { "/system/app/Superuser.apk", "/sbin/su", "/system/bin/su", "/system/xbin/su", "/data/local/xbin/su", "/data/local/bin/su", "/system/sd/xbin/su",
"/system/bin/failsafe/su", "/data/local/su", "/su/bin/su"};
for (String path : paths) {
if (new File(path).exists()) return true;
}
return false;
}
private static boolean checkRootMethod3() {
Process process = null;
try {
process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
if (in.readLine() != null) return true;
return false;
} catch (Throwable t) {
return false;
} finally {
if (process != null) process.destroy();
}
}
}
网友评论