2
I’m developing a app and I’m looking to implement a method that makes your screen never erase.
I did some research on the Internet and I was able to do this inside the app. However, I would like this feature to continue working when I minimize the app, always keeping your phone on screen even using other applications.
My goal is to do something like this: youtube.com/watch? v=ywXg3cqL8Tw
I thought I’d use an asynchronous method next to WAKE_LOCK
, but I couldn’t.
I saw by the examples as the app of the link provided above to make it work with the app off, it is using a class extends Servise
that with this he can keep the screen lit even with the app disconnected.
Below you can see my code, tried by onBind
, but I couldn’t either:
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 anyone knows how to help me keep the screen on, any tips are welcome!
What minimum api you set for your application?
– user28595
It’s the pattern of when you create a project, I believe it’s Froyo 2.2
– CristianCotrena
I have a project here with the same command that you are using and works normally. However, I added directly to
onCreate()
.– user28595
but when you minimize the application it keeps working? IE, if you leave on the home screen of Android for example, it always stays on?
– CristianCotrena
If you leave the screen, of course it won’t work. I suggest you take a look at android lifecycle. Here has a good explanation too.
– user28595
Perhaps it would be more interesting for you to tell us what you want to do with it. Perhaps there is another solution. For example, your intention is to keep some operation running?
– Pablo Almeida
Thanks for the attention guys, basically I just want to activate the screen so that it is always on I know you can do by the settings on Android itself, but I wanted to do this by the app. my goal is to be able to minimize the app and the phone screen is always on, and when I want to deactivate, I go back to the app and deactivate
– CristianCotrena
From what I have seen, I really cannot because of the life cycle of the activity. I thank you for your help. But in case you can help me with another quick question, I’d really appreciate it. I know there is this feature to leave the Android screen always on by your settings, would I change this information permanently by the app? Basically, I wanted to do something like this: https://www.youtube.com/watch?v=ywXg3cqL8Tw Hug!
– CristianCotrena