1

I am building a lock screen app. I want to place a floating icon on Homescreen, which when clicked will lock the phone. I was able to implement the functionality of Locking the Phone using Device Manager API. Now I want to add a Floating Icon on Homescreen.

I tried using the solution recommended here: What APIs are used to draw over other apps (like Facebook's Chat Heads)?

But this does not seem to be working in my case. It is not diplaying an icon on the homescreen ever after gaining SYSTEM OVERLAY Permission.

LockButtonService.java

public class LockButtonService extends Service {

    private WindowManager windowManager;
    private ImageView chatHead;

    @Override public IBinder onBind(Intent intent) {
        // Not used
        return null;
    }

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

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        chatHead = new ImageView(this);
        chatHead.setImageResource(R.drawable.lock);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;

        windowManager.addView(chatHead, params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (chatHead != null) windowManager.removeView(chatHead);
    }
}

MainActivity.java

add_homescreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Code For Adding Shortcut Icon On Home Screen
//                if (ShortcutManagerCompat.isRequestPinShortcutSupported(getApplicationContext())) {
//                    ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(getApplicationContext(), "#1")
//                            .setIntent(new Intent(getApplicationContext(), LockActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
//                            .setShortLabel("Lock Screen")
//                            .setIcon(IconCompat.createWithResource(getApplicationContext(), R.drawable.lock))
//                            .build();
//                    ShortcutManagerCompat.requestPinShortcut(getApplicationContext(), shortcutInfo, null);
//                } else {
//                    Toast.makeText(MainActivity.this,"launcher does not support short cut icon",Toast.LENGTH_LONG).show();
//                }

                    //Code For Requesting System Overlay


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (!Settings.canDrawOverlays(getApplicationContext())) {
                        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                        startActivityForResult(intent, 12345);
                    }
                    else {
                        //Code To Execute If The Permission Is Granted
                        Toast.makeText(getApplicationContext(),"Permission Granted",Toast.LENGTH_LONG);
                       startService(new Intent(getApplicationContext(), LockButtonService.class));
                    }
                }




            }
SK707
  • 381
  • 1
  • 3
  • 8

1 Answers1

1

I figured out the issue. It seems like I forgot to declare this service in Android Manifest. I was using WindowManager.LayoutParams.TYPE_PHONE in LockButtonService.java.

I have now changed it into WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, and my icon is displaying on homescreen and all other activities.

SK707
  • 381
  • 1
  • 3
  • 8