How to leave the screen always on even when closing the app

2

I'm developing an app and I'm trying to implement a method that will make your screen never go away.

I researched some forms on the internet and was able to do this within the application. However, I would like this feature to continue to work when I minimize the application, always keeping the phone on the screen on even using other applications.

My goal is to do something like this: youtube.com/watch?v=ywXg3cqL8Tw

I thought about using an asynchronous method with WAKE_LOCK , but I could not.

I have seen examples like the app of the link provided above that to work with the app off, it is using a extends Servise class that it can do keep the screen lit even with the app off.

Below you can see my code, I tried for onBind , but I could not:

public class ServiceScreen extends Service {
    private static final String TAG = "Service";

    SharedPreferences prefs;
    SharedPreferences.Editor editor;
    Notification n;
    NotificationManager notificationManager;
    // Change this int to some number specifically for this app
    int notifId = 5315;
    //Screen luz;

    IBinder b;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        //getCurrentFocus().setKeepScreenOn(true);
        //int x = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        arg0.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        return b;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
        // Init the SharedPreferences and Editor

        prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        editor = prefs.edit();

        notificationManager = (NotificationManager) getApplicationContext()
            .getSystemService(NOTIFICATION_SERVICE);
        Context context = getApplicationContext();

        String notifTitle = context.getResources().getString(R.string.app_name);
        String notifMessage = context.getResources().getString(R.string.ligado);
        n = new Notification();
        n.icon = R.drawable.ic_launcher;
        n.tickerText = "Buffering";
        n.when = System.currentTimeMillis();

        Intent nIntent = new Intent(context, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0);

        n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent);
        notificationManager.notify(notifId, n);
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onStart(Intent intent, int startId) {
        Context context = getApplicationContext();
        String notifTitle = context.getResources().getString(R.string.app_name);
        String notifMessage = context.getResources().getString(R.string.ligado);

        n.icon = R.drawable.ic_launcher;
        n.tickerText = notifMessage;
        n.flags = Notification.FLAG_NO_CLEAR;
        n.when = System.currentTimeMillis();
        Intent nIntent = new Intent(context, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0);
        n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent);
        notificationManager.notify(notifId, n);
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        notificationManager.cancel(notifId);
    }
}

If someone knows how to help keep the screen lit, all tips are welcome!

    
asked by anonymous 08.01.2016 / 18:57

1 answer

5

I was able to do it here! so I can turn off the app that it follows while keeping the screen switched on.

I added in the onCreate of class ServiceScreen this code:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "watever");
wl.acquire();
    
13.01.2016 / 17:38