| | |
| | | package com.auto.lyric; |
| | | |
| | | import android.content.Intent; |
| | | import android.graphics.PixelFormat; |
| | | import android.net.Uri; |
| | | import android.os.Build; |
| | | import android.os.Handler; |
| | | import android.os.Message; |
| | | import android.provider.Settings; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | import android.view.Gravity; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | import android.view.WindowManager; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | |
| | | import com.auto.lyric.base.activities.BaseActivity; |
| | | import com.auto.lyric.databinding.ActivityMainBinding; |
| | | import com.auto.lyric.databinding.FloatViewBinding; |
| | | import com.auto.lyric.service.AutoInputService; |
| | | import com.auto.lyric.service.FloatingWindowService; |
| | | import com.auto.lyric.vm.MainViewModel; |
| | | |
| | | public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> { |
| | | |
| | | Intent service = new Intent(getApplicationContext(), AutoInputService.class); |
| | | |
| | | @Override |
| | | public void initViews() { |
| | | |
| | | } |
| | | Intent inputService = new Intent(getApplicationContext(), AutoInputService.class); |
| | | |
| | | @Override |
| | | protected void onResume() { |
| | | super.onResume(); |
| | | Intent floatService = new Intent(getApplicationContext(), FloatingWindowService.class); |
| | | binding.button.setOnClickListener(v -> { |
| | | |
| | | //申请权限 |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| | | //开启悬浮窗 |
| | | if(!Settings.canDrawOverlays(getApplicationContext())) { |
| | | //启动Activity让用户授权 |
| | | Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); |
| | | intent.setData(Uri.parse("package:" + getPackageName())); |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
| | | startActivity(intent); |
| | | }else if(!isAccessibilitySettingsOn()){//辅助权限 |
| | | Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
| | | startActivity(intent); |
| | | }else{//开启监听服务 |
| | | startService(service); |
| | | //申请权限 |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| | | //开启悬浮窗 |
| | | /* if(!Settings.canDrawOverlays(getApplicationContext())) { |
| | | //启动Activity让用户授权 |
| | | Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); |
| | | intent.setData(Uri.parse("package:" + getPackageName())); |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
| | | startActivity(intent); |
| | | }else if(!isAccessibilitySettingsOn()){//辅助权限 |
| | | Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
| | | startActivity(intent); |
| | | }else{//开启监听服务 |
| | | //startService(floatService); |
| | | }*/ |
| | | startService(floatService); |
| | | //showFloatView(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | public boolean isAccessibilitySettingsOn() { |
| | |
| | | return false; |
| | | } |
| | | |
| | | private void showFloatView(){ |
| | | |
| | | |
| | | //View view = LayoutInflater.from(this).inflate(R.layout.float_view,null); |
| | | FloatViewBinding binding = FloatViewBinding.inflate(getLayoutInflater()); |
| | | WindowManager manager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE); |
| | | WindowManager.LayoutParams params = new WindowManager.LayoutParams(); |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| | | params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; |
| | | }else { |
| | | params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; |
| | | } |
| | | params.format = PixelFormat.RGBA_8888; |
| | | params.gravity = Gravity.CENTER; |
| | | params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
| | | params.width = WindowManager.LayoutParams.MATCH_PARENT; |
| | | params.height = WindowManager.LayoutParams.WRAP_CONTENT; |
| | | binding.btnBack.setOnClickListener(v ->{ |
| | | stopService(new Intent(this,this.getClass())); |
| | | }); |
| | | manager.addView(binding.getRoot(),params); |
| | | } |
| | | |
| | | } |