我正在开发一款安全应用程序,可防止手机关机(内置电池的手机)。我发现stackoverflow在启动项目时非常有用,但是在下面的代码中存在问题。 这个代码正在Android模拟器和我兄弟的手机摩托罗拉X Play上工作,但它既不能用于我的手机小米MI 5,也不适用于我朋友的手机小米MI4i
无法在我的手机中使用,因此,关机菜单不会被解雇 而这段代码习惯于在2-3周前在每部手机中工作。
请有人帮助我完成这项工作,以便我可以继续开发我的项目,这样我就可以为我们的移动设备安全迈出一步 提前谢谢:)
public class MainActivity extends AppCompatActivity {
public final static int REQUEST_CODE = 10101;
Handler H;
Runnable Run;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkDrawOverlayPermission()) {
startService(new Intent(this, MyService.class));
}
}
public boolean checkDrawOverlayPermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
if (!Settings.canDrawOverlays(this)) {
/** if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
/** request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
return false;
} else {
return true;
}
}
@Override
@TargetApi(Build.VERSION_CODES.M)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (Settings.canDrawOverlays(this)) {
startService(new Intent(this, MyService.class));
}
}
}
}
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(getApplicationContext(),"Service Running",Toast.LENGTH_LONG).show();
LinearLayout mLinear = new LinearLayout(getApplicationContext()) {
//home or recent button
public void onCloseSystemDialogs(String reason)
{
if ("globalactions".equals(reason)) {
Log.i("*********","********Long press on power button");
KeyguardManager kgm = (KeyguardManager) getSystemService(getApplicationContext().KEYGUARD_SERVICE);
if(kgm.isKeyguardLocked()) {
sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}
}
}
};
mLinear.setFocusable(true);
View mView = LayoutInflater.from(this).inflate(R.layout.service_layout, mLinear);
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
0,
0,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.CENTER | Gravity.TOP;
mWindowManager.addView(mView, mParams);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}