Salvo la MainActivity, notevole specialmente per il "trucchetto" che evita di dover riavviare l'applicazione dopo i permessi a runtime:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Runnable runnable = new Runnable() { @RequiresApi(api = Build.VERSION_CODES.M) @Override public void run() { if(Settings.canDrawOverlays(getApplicationContext())){ Intent i = new Intent(getApplicationContext(),MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(i); }else{ new Handler().postDelayed(this,1000); } } }; if(Build.VERSION.SDK_INT >= 23 ){ if(!Settings.canDrawOverlays(getApplicationContext())){ Intent i = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:"+getPackageName())); startActivity(i); new Handler().postDelayed(runnable,1000); } } Intent intent=new Intent(getApplicationContext(),Servizio.class); startService(intent); } }
Salvo il codice dello studio teorico:
public class Servizio extends Service { int x_init_coord, y_init_coord, x_margin, y_margin; long time_start, time_end; private View mFloatingView; private View removeView; private WindowManager windowManager; private WindowManager.LayoutParams wParams; public Servizio() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public void onCreate() { super.onCreate(); windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); addFloatingWidgetView(); addRemoveWidgetView(); } private void addFloatingWidgetView() { mFloatingView = LayoutInflater.from(this).inflate(R.layout.widget, null); wParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT ); wParams.gravity = Gravity.TOP | Gravity.LEFT; wParams.x = 0; wParams.y = 100; windowManager.addView(mFloatingView, wParams); mFloatingView.setOnTouchListener(new View.OnTouchListener() { int x_init_coord, y_init_coord; int x_margin, y_margin; @Override public boolean onTouch(View view, MotionEvent event) { WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) mFloatingView.getLayoutParams(); int x_coord = (int) event.getRawX(); int y_coord = (int) event.getRawY(); switch(event.getAction()){ case MotionEvent.ACTION_DOWN: time_start = System.currentTimeMillis(); x_margin = layoutParams.x; y_margin = layoutParams.y; x_init_coord = x_coord; y_init_coord = y_coord; return true; case MotionEvent.ACTION_MOVE: int x_diff = x_coord - x_init_coord; int y_diff = y_coord - y_init_coord; int x_destination = x_margin + x_diff; int y_destination = y_margin + y_diff; layoutParams.x = x_destination; layoutParams.y = y_destination; windowManager.updateViewLayout(view,layoutParams); return true; case MotionEvent.ACTION_UP: int x_diff_up = x_coord - x_init_coord; int y_diff_up = y_coord - y_init_coord; if(Math.abs(x_diff_up) < 5 && Math.abs(y_diff_up) < 5){ time_end = System.currentTimeMillis(); if(time_end - time_start < 300){ System.out.println("E' UN CLICK"); } } } return false; } }); } private void addRemoveWidgetView(){ LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); removeView = inflater.inflate(R.layout.removewidget,null); WindowManager.LayoutParams removeParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT ); removeParams.gravity = Gravity.TOP | Gravity.LEFT; windowManager.addView(removeView,removeParams); } }
Salvo i due xml che vengono "inflatati" per la floatingView e per la removeView:
floatingVIew:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content"> <!--Root container--> <RelativeLayout android:id="@+id/root_container" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:ignore="UselessParent"> <ImageView android:adjustViewBounds="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src = "@drawable/facciadascemo"/> </RelativeLayout> </FrameLayout>
removeView:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content"> <!--Root container--> <RelativeLayout android:id="@+id/root_container" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:ignore="UselessParent"> <ImageView android:adjustViewBounds="true" android:layout_width="50dp" android:layout_height="50dp" android:src = "@drawable/removeimage"/> </RelativeLayout> </FrameLayout>
Nessun commento:
Posta un commento