如何使用compile sdk 25禁用所有硬件按钮

时间:2017-08-11 07:31:52

标签: android lockscreen

我现在正在为Android做一个锁屏我希望在默认锁定屏幕上显示它我从我的活动和服务i trigered服务我称为接收器,当SCREEN_ON SCREEN _OFF,ACTION USER PRESENT时调用。 一切都很好,直到我按下主页按钮,以控制主页按钮我googled有很多解决方案我尝试了一切,但没有任何工作。 How to disable virtual home button in any activity? 在此发现我想将我的活动作为窗口管理器活动。所以搜索了如何制作它。  我发现了这个

  WindowManager mWindowManager;
   WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
    //here is all the science of params
    final WindowManager.LayoutParams myParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY ,
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            PixelFormat.TRANSLUCENT
    );

   /* WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
            WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
            PixelFormat.TRANSLUCENT
    );*/
    myParams.gravity = Gravity.TOP;

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout mOverlay = (RelativeLayout) inflater.inflate(R.layout.activity_main, (ViewGroup) null);
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    mWindowManager.addView(mOverlay, myParams);

当我在我的服务的onstart命令中添加此项时,我发现了此错误

       Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@fa59ecd -- permission denied for window type 2006

当我用Google搜索时,我发现我需要在运行时检查canoverlay权限,所以我在触发myservice之前在mainactivity中检查这个,但即使现在我也无法控制我的homebutton。由于我的锁屏是必须的。我在Playstore中发现了很多锁屏应用程序。如果可能的话怎么做。 我的代码: 活性

 public class MainActivity extends AppCompatActivity {
HomeKeyLocker homeKeyLocker ;
boolean    askedForOverlayPermission;
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
public final static int OVERLAY_PERMISSION_CODE = -1010101;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    if(Build.VERSION.SDK_INT >= 23) {
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 1234);
        }
    }
    else {
        Intent intent = new Intent(this, Myservice.class);
        startService(intent);
    }

}

public void req(View view)
{

        android.os.Process.killProcess(android.os.Process.myPid());
    }
protected void onPause() {
    super.onPause();
    Intent intent = new Intent(this,MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
} 

我的清单:

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:clearTaskOnLaunch="true"
    android:stateNotNeeded="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"

        android:launchMode="singleTask"
        android:excludeFromRecents="true"
        android:screenOrientation="nosensor">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".Receiverb">
        <intent-filter>
            <action android:name="android.intent.action.USER_PRESENT">
            </action>
            <action android:name="android.intent.action.SCREEN_OFF">
            </action>
            <action android:name="android.intent.action.BOOT_COMPLETED">
            </action>
        </intent-filter>
    </receiver>
    <service android:name=".Myservice"/>
</application>

我的接收者:

public class Receiverb extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("receiver", "main");

    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        // do whatever you need to do here
        Log.d("receiver", "screen off");
        context.startActivity(new Intent(context,
                MainActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        // and do whatever you need to do here
        Log.d("receiver", "screen on");
        context.startActivity(new Intent(context,
                MainActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        Log.d("receiver", "aft activity");
    } else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
        Log.d("receiver", "unlock");

        context.startActivity(new Intent(context,
                MainActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }
}

0 个答案:

没有答案