我一直试图做锁定任务模式或锁定到我的应用程序,而不能访问其他应用程序仍然能够禁用该功能。我看过教程和文档,但有些方法是在大约5年前(onActivityResult)制作的,所以不推荐使用。我已经尝试更改为我发现的新方法(registerForActivityResult),但是当我启动我的应用程序并尝试通过点击启用按钮来启用管理员权限时,管理员权限不会弹出,所以我无法检查它。所以,当我试图从其他应用程序锁定我的手机什么也没发生,因为我没有管理员权限.我的代码MainActivity.class如下:
package com.example.myapplication;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityOptions;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.myapplication.databinding.ActivityMainBinding;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.myapplication.helper.MqttHelper;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttMessage;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button lock, disable, enable;
private static final int RESULT_ENABLE = 11;
private DevicePolicyManager devicePolicyManager;
private ActivityManager activityManager;
private ComponentName componentName;
private AppBarConfiguration appBarConfiguration;
private ActivityMainBinding binding;
MqttHelper mqttHelper;
//ChartHelper mChart;
//LineChart chart;
TextView dataReceived;
private Button btnPublish;
// Allowlist two apps.
private static final String KIOSK_PACKAGE = "com.example.myapplication";
private static final String[] APP_PACKAGES = {KIOSK_PACKAGE};
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
//setContentView(R.layout.activity_main);
devicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
componentName = new ComponentName(this, Controller.class);
lock = (Button) findViewById(R.id.lock);
enable = (Button) findViewById(R.id.enable);
disable = (Button) findViewById(R.id.disable);
lock.setOnClickListener(this);
enable.setOnClickListener(this);
disable.setOnClickListener(this);
dataReceived = (TextView) findViewById(R.id.textview_first);
startMqtt();
}
@Override
protected void onResume() {
super.onResume();
boolean isActive = devicePolicyManager.isAdminActive(componentName);
}
@Override
public void onClick(View view) {
if (view == lock) {
boolean active = devicePolicyManager.isAdminActive(componentName);
if(active) {
devicePolicyManager.lockNow();
} else {
Toast.makeText(this, "You need to enable the Admin Device Features", Toast.LENGTH_SHORT).show();
}
} else if (view == enable) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"You Should Enable the app!");
//someActivityResultLauncher.launch(intent);
// Caller
mGetContent.launch(intent);
// Receiver
//startActivityForResult(intent, RESULT_ENABLE);
} else if (view == disable) {
devicePolicyManager.removeActiveAdmin(componentName);
disable.setVisibility(View.GONE);
enable.setVisibility(View.VISIBLE);
}
}
ActivityResultLauncher<Intent> mGetContent = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
// Here, no request code
Intent data = result.getData();
Toast.makeText(MainActivity.this, "enabled", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode== Activity.RESULT_OK && requestCode == RESULT_ENABLE){
Toast.makeText(MainActivity.this, "enabled", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
/*@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case RESULT_ENABLE:
if(resultCode== Activity.RESULT_OK ){
//btn_unblock.setText("Dissable");
//btn_block.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this, "enabled", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
// MQTT Code
private void startMqtt(){
mqttHelper = new MqttHelper(getApplicationContext());
mqttHelper.mqttAndroidClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectComplete(boolean b, String s) {
Log.w("Debug","Connected");
}
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
Log.w("Debug",mqttMessage.toString());
dataReceived.setText(mqttMessage.toString());
//mChart.addEntry(Float.valueOf(mqttMessage.toString()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
}
}
1条答案
按热度按时间dgsult0t1#
我目前正在开发一个企业应用程序,它运行在平板电脑上,也处于锁定任务模式,所以我分享了我为我的案例所做的事情。不是一个超级干净的代码,但它做的工作。
第一个java类:
你需要一个像这样的管理接收器类
如果你让它空着也没关系。
第二个manifest.xml:
添加此接收器到您的清单和编辑标签和描述的基础上你想要的。
你应该通过adb将你的应用程序设置为admin,因为我也无法在设置中启用它,所以我目前使用adb命令。
{packageName}:您可以在标记为“applicationId”的gradle文件中找到packageName。
{your管理类}:您的类可能类似于com.example.appName.AppAdminReceiver
最后一件事任务锁类,使事情变得容易:
让我来解释一下:
首先u调用initialize并传递它需要的输入。然后你可以很容易地调用stopKiosk或startKiosk来做你的事情。
例如:
注意:要安装新版本或删除应用程序,您必须使用adb命令删除设备的所有权。