Java 如何在android锁屏上添加图像滑块视图

Java 如何在android锁屏上添加图像滑块视图,java,android,android-windowmanager,Java,Android,Android Windowmanager,我的代码 public class SecondTextView extends Service { private BroadcastReceiver mReceiver; private boolean isShowing = false; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } private WindowMan

我的代码

public class SecondTextView extends Service {

private BroadcastReceiver mReceiver;
private boolean isShowing = false;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

private WindowManager windowManager;
private ImageView floatIcon;
WindowManager.LayoutParams params;

@Override
public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);

    //add floatIcon and its properties;
   /* floatIcon = new TextView(this);
    floatIcon.setText("Hello There!");
    floatIcon.setTextColor(ContextCompat.getColor(this, android.R.color.white));
    floatIcon.setTextSize(32f);*/;
    floatIcon = new ImageView(this);
    floatIcon.setImageResource(R.drawable.home);
    floatIcon.setClickable(true);

    //set parameters for the floatIcon;
    params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.TOP|Gravity.CENTER_HORIZONTAL;

    //Register receiver for determining screen off and if user is present;
    mReceiver = new LockScreenStateReceiver();
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_USER_PRESENT);

    registerReceiver(mReceiver, filter);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

public class LockScreenStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            //if screen is turn off show the floatIcon;
            if (!isShowing) {
                windowManager.addView(floatIcon, params);
                isShowing = true;
            }
        }

        else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
            //Handle resuming events if user is present/screen is unlocked remove the floatIcon immediately;
            if (isShowing) {
                windowManager.removeViewImmediate(floatIcon);
                isShowing = false;
            }
        }
    }
}

@Override
public void onDestroy() {
    //unregister receiver when the service is destroy;
    if (mReceiver != null) {
        unregisterReceiver(mReceiver);
    }

    //remove view if it is showing and the service is destroy;
    if (isShowing) {
        windowManager.removeViewImmediate(floatIcon);
        isShowing = false;
    }
    super.onDestroy();
}
}
我使用了这段代码,但给出了错误并使我的应用程序崩溃

android.view.WindowManager$BadTokenException:无法添加窗口android.view.ViewRootImpl$W@bed76c6

AndroidManifest.xml


那么,要求是什么?为什么以及你们想在锁屏上显示什么?我想在锁屏上显示图像滑块@提瓦里艾什瓦里亚酒店
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.imageonlockscreen" >
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".SecondTextView"/>

    <service android:name=".FloatIcon"/>
</application>