1
package com.vkstechnologies.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.widget.Toast;

/**
 * Created by Vipul-Laptop on 03-01-2018.
 */

public class MyService extends Service {


    @Nullable
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {

        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(!hasFocus) {
            Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialog);
        }
    }



}

I want to make an application that runs in background and detects whenever a power key is pressed to shut down the phone and it just dismisses the "power off" window. If I run this method in MainActivity, then it runs smoothly, but not in service. The Error says : cannot resolve method onWindowFocusChanged

Vipul Kumar
  • 221
  • 2
  • 11

0 Answers0