У меня есть значок с плавающей точкой, который должен отображаться поверх всех действий и должен отображаться, когда устройство находится в заблокированном состоянии.Я поддерживаю состояние включения / выключения экрана через приемник вещания.У меня есть код, чтобы скрыть при разблокированном состоянии и показать на устройстве заблокированное состояние.Но плавающее действие не отображается, когда экран выключен.Я сохранил журнал на метод onReceive, журнал был показан при включении / выключении экрана.Некоторые из руководств предлагают сохранить WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, пока я оставляю этот флаг, тогда приложение перестало работать, отказывая в разрешении на выброс.И когда я заменяю TYPE_SYSTEM_ERROR на TYPE_APPLICATION_OVERLAY, то плавающее изображение не отображается на заблокированном экране.Кто-нибудь может мне подсказать, как решить эту проблему?9 У меня есть значок с плавающей точкой, который отображается поверх всех действий, но когда устройство заблокировано, оно исчезает до тех пор, пока устройство не разблокируется.
Еще одно значение - я хочу отобразить представление (FloatIcon) на экране блокировки, используяwindowmanager из службы.
Это мой код.
package com.example.accessibilityservice;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.os.Vibrator;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
public class FloatingIconService extends Service {
private FloatingIconReceiver mReceiver;
private boolean isShowing = false;
private WindowManager windowManager;
public ImageView icon;
private WindowManager.LayoutParams params;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("onStart", "FloatingIconService");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
//add icon and its properties
icon = new ImageView(this);
icon.setImageResource(R.drawable.ic_home);
icon.setPadding(10, 10, 10, 10);
icon.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Log.i("onStart", "FloatingIconService when long press on icon");
((Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE)).vibrate(1000);
/*Intent i = new Intent(getApplicationContext(), Dashboard.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);*/
return true;
}
});
//set parameters for the icon
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 10;
params.y = 100;
//Register receiver for determining screen off and if user is present
mReceiver = new FloatingIconReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
registerReceiver((BroadcastReceiver) mReceiver, filter);
Log.i("onStart", "FloatingIconService2");
icon.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(icon, params);
return true;
}
return false;
}
});
}
public class FloatingIconReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("TEST", "TEST");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//if screen is turn off show the icon
if (!isShowing) {
windowManager.addView(icon, params);
isShowing = true;
}
} else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
//Handle resuming events if user is present/screen is unlocked remove the icon immediately
if (isShowing) {
windowManager.removeViewImmediate(icon);
isShowing = false;
}
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("onStart", "FloatingIconService3");
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
//remove view if it is showing and the service is destroy
if (isShowing) {
windowManager.removeViewImmediate(icon);
isShowing = false;
}
super.onDestroy();
}
}
Код класса активности:
startService(new Intent(getApplication(), FloatingIconService.class));
Файл манифеста:
<receiver android:name=".FloatingIconService$FloatingIconReceiver" android:enabled="true" android:exported="true" />
<service android:name=".FloatingIconService" />