Вопрос

I want create icon over screen that just listen for touch on it (without forced to create useless notification) after 3 days search over web and ask in IRC channels and implement must examples like this:

Android app that runs on top of ALL other apps?

Creating a system overlay window (always on top)

i found there is a hacky way to do this, but no one know it (except Smart Taskbar developers) i install Smart Taskbar with most source examples and found this examples killed without notification bar , but smart taskbar never killed ,(i found you can't even kill it with regular task killer, like Android Task Manager) , there is just one way to kill this icon , go to settings> apps> select smart taskbar >Force stop.

another thing that i found is that it never go to background and in android task manager (in real time process list) it is marked as visible and you can't kill it can someone help me how can i create an icon that are always visible and never killed over screen

Это было полезно?

Решение

Call setupNotification() in your service's onCreate method and clearNotification() in your service's onDestroy method.

private int SERVICE_NOTIFICATION = 1; // this should be unique for your app

private void setupNotification()
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
        Notification serviceNotification = new NotificationCompat.Builder(this)
            .setContentTitle("Your Service")
            .setContentText("Your Service is running in the background")
            .setSmallIcon(R.drawable.ic_launcher)
            .setPriority(NotificationCompat.PRIORITY_MIN)
            .setOngoing(true)
            .build();
        startForeground(SERVICE_NOTIFICATION, serviceNotification);
    }
    else{
        Notification serviceNotification = new Notification();
        serviceNotification.flags = Notification.FLAG_ONGOING_EVENT;
        startForeground(SERVICE_NOTIFICATION, serviceNotification);
    }
}

private void clearNotification()
{
    stopForeground(true);
}

Другие советы

This is absolutely doable. You should keep an always running service. and in on create of that service,

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

        customView= new MYCustomView(MyService.this);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.RIGHT | Gravity.TOP;
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(customView, params);
    }

This will place your view on top of all vies.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top