网络设备管理相关相关
1 WifiManager LocationManager分别控制 wifi及GPS
WifiManager 判断网络状态 ,LocationManager判断定位状态,ConnectivityManager 网络信息
public class Wifi_LocationActivity extends AppCompatActivity { private TextView tv_wifiStatus, tv_gpsStatus;
private Button btn_startWifi, btn_startGps, btn_startGame; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi__location);
findViews();
setListener();
checkWifiAndGpsStatus();
} private void findViews() {
tv_wifiStatus = findViewById(R.id.tv_wifistatus);
tv_gpsStatus = findViewById(R.id.tv_gpsstatus);
btn_startWifi = findViewById(R.id.btn_startwifi);
btn_startGps = findViewById(R.id.btn_startGPS);
btn_startGame = findViewById(R.id.btn_startGame);
} private void setListener() {
btn_startWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
if (android.os.Build.VERSION.SDK_INT <= 11) {
intent.setClassName("com.android.settings", "com.android.settings.Settings$WifiSettingsActivity");
} else {
intent.setClassName("com.android.settings", "com.android.settings.wifi.WifiSettings");
}
startActivityForResult(intent, 0);
} });
btn_startGps.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0);
}
});
btn_startGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(Wifi_LocationActivity.this)
.setTitle("准备")
.setMessage("准备进入游戏")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
showWifiType();
}
}).show();
}
});
} private void checkWifiAndGpsStatus() {
boolean result = true;
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
if (!wifiManager.isWifiEnabled() && wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
tv_wifiStatus.setText("未启动");
result = false;
} else {
tv_wifiStatus.setText("已启动");
} if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
|| !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
tv_gpsStatus.setText("未启动");
result = false;
} else {
tv_gpsStatus.setText("已启动");
}
if (result) {
btn_startGame.setEnabled(true);
} else {
btn_startGame.setEnabled(false);
}
} @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
checkWifiAndGpsStatus();
} private void showWifiType() {
ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() == null) {
tv_wifiStatus.setText("null");
} else {
if (cm.getActiveNetworkInfo().isAvailable()) {
tv_wifiStatus.setText(":" + cm.getActiveNetworkInfo().getTypeName());
} else {
tv_wifiStatus.setText("null");
}
}
} }
权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
2.设备管理 DevicePolicyManager
参照地址 https://blog.csdn.net/black_bird_cn/article/details/79735794
SaveDeviceActivity 打开和关闭管理员权限
让当前应用带管理员权限
public class SaveDeviceActivity extends Activity{ private DevicePolicyManager myDPM;
private ComponentName myDeviceAdminReceiver;
private Button btn_startDevice;
private Button btn_setDevice;
private Button btn_stopDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save_device);
myDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
myDeviceAdminReceiver = new ComponentName(this,MyDeviceAdminReceiver.class);
findViews();
setListener();
}
private void findViews()
{
btn_startDevice = findViewById(R.id.btn_startDevice);
btn_setDevice = findViewById(R.id.btn_setDevice);
btn_stopDevice = findViewById(R.id.btn_stopDevice);
} private void setListener()
{
btn_startDevice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("panzqww","start Device+++++++++++++++++++++");
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,myDeviceAdminReceiver);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"提别说明:此画面为启动设备管理画面");
startActivityForResult(intent,1);
}
});
btn_setDevice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("panzqww","set Device+++++++++++++++++++++");
Intent intent2 = new Intent();
intent2.setClass(SaveDeviceActivity.this,DeviceSettingActivity.class);
intent2.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,myDeviceAdminReceiver);
startActivity(intent2);
}
});
btn_stopDevice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("panzqww","stop Device+++++++++++++++++++++");
myDPM.removeActiveAdmin(myDeviceAdminReceiver);
updateButtonStatus();
}
});
} private void updateButtonStatus() {
boolean active = myDPM.isAdminActive(myDeviceAdminReceiver);
if (active)
{
btn_startDevice.setEnabled(false);
btn_setDevice.setEnabled(true);
btn_stopDevice.setEnabled(true);
}else{
btn_startDevice.setEnabled(true);
btn_setDevice.setEnabled(false);
btn_stopDevice.setEnabled(false);
}
} @Override
protected void onResume() {
super.onResume();
updateButtonStatus();
} @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode)
{
case RESULT_OK:
Toast.makeText(SaveDeviceActivity.this,"启动成功",Toast.LENGTH_LONG).show();
updateButtonStatus();
break;
default:
Toast.makeText(SaveDeviceActivity.this,"取消启动",Toast.LENGTH_LONG).show();
updateButtonStatus();
break;
}
} }
MyDeviceAdminReceiver
public class MyDeviceAdminReceiver extends DeviceAdminReceiver {
private void showToast(Context context,CharSequence msg)
{
Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
} @Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
showToast(context,"DeviceAdminReceiver 启动");
} @Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return "DeviceAdminReceiver 是否确定停用";
} @Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
showToast(context,"DeviceAdminReceiver 停用");
} @Override
public void onPasswordChanged(Context context, Intent intent, UserHandle user) {
super.onPasswordChanged(context, intent, user);
showToast(context,"DeviceAdminReceiver 修改密码");
} @Override
public void onPasswordFailed(Context context, Intent intent, UserHandle user) {
super.onPasswordFailed(context, intent, user);
showToast(context,"DeviceAdminReceiver 密码错误");
} @Override
public void onPasswordSucceeded(Context context, Intent intent, UserHandle user) {
super.onPasswordSucceeded(context, intent, user);
showToast(context,"DeviceAdminReceiver 密码正确");
}
}
AndroidManifest.xml中配置广播接收器
<receiver
android:name=".MyDeviceAdminReceiver"
android:description="@string/app_name"
android:label="@string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin" /> <intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
新建xml目录并新建device_admin.xml文件
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
<expire-password />
<encrypted-storage />
<disable-camera />
</uses-policies>
</device-admin>
DeviceSettingActivity 实现DevicePolicyManager的一些具体操作
public class DeviceSettingActivity extends Activity implements View.OnClickListener{
private DevicePolicyManager myDPM;
private Button btn_lock,btn_resetPassword,btn_wipeData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_setting);
myDPM=(DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
findViews();
}
private void findViews()
{
btn_lock = findViewById(R.id.btn_lock);
btn_lock.setOnClickListener(this);
btn_resetPassword = findViewById(R.id.btn_resetPassword);
btn_resetPassword.setOnClickListener(this);
btn_wipeData = findViewById(R.id.btn_wipeData);
btn_wipeData.setOnClickListener(this);
} @Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.btn_lock:
myDPM.lockNow();
break;
case R.id.btn_resetPassword:
/**
*当设置此flag时,resetPassword在锁屏状态下失去重置作用,即任何admin用户都必须先进入系统才能重置密码
public static final int RESET_PASSWORD_REQUIRE_ENTRY = 0x0001;
使用此flag必须是device owner,可在不需要密码的情况下启动设备,暂不清楚用法
public static final int RESET_PASSWORD_DO_NOT_ASK_CREDENTIALS_ON_BOOT = 0x0002;
设为0表示可任意重置密码(无论是否解锁)
public static final int PASSWORD_QUALITY_UNSPECIFIED = 0;
*/
myDPM.resetPassword("",0);//如果要清除锁屏密码直接传入空字符。可能会报错,需要重启一下手机就可以
break;
case R.id.btn_wipeData:
/**
//双清存储数据(包括外置sd卡),wipeData后重启
public static final int WIPE_EXTERNAL_STORAGE = 0x0001;
//恢复出厂设置,使用此flag必须是device owner,否则将抛出SecurityException异常,而setDeviceOwner为隐藏API
public static final int WIPE_RESET_PROTECTION_DATA = 0x0002;
*/
myDPM.wipeData(1);
break;
}
}
}
3.执行adb shell的命令
public class ADBShellActivity extends AppCompatActivity { TextView tv_content;
EditText et_command;
Button btn_startShell;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adbshell);
findViews();
setListener();
}
private void findViews()
{
tv_content = findViewById(R.id.tv_content);
et_command =findViewById(R.id.et_command);
btn_startShell = findViewById(R.id.btn_startShell);
et_command.setText("pm list package");
}
private void setListener()
{
btn_startShell.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String command = et_command.getText().toString();
if (!TextUtils.isEmpty(command))
{
tv_content.setText(command+":");
runAdbShellCommand(command);
}
}
});
}
private void runAdbShellCommand(String command)
{
Log.d("panzqww",command);
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while((line = reader.readLine())!=null)
{
Log.d("panzqww","line = "+line);
output.append(line).append("\n");
}
reader.close();
int status = process.waitFor();
Log.d("panzqww","status" +status);
tv_content.setText(output.toString());
} catch (Exception e) {
e.printStackTrace();
tv_content.setText(command+":"+"权限不足");
}finally {
if (process != null)
{
process.destroy();
process = null;
}
} }
}