hi i need to keep the device screen on mood, while application running.
disable the power button functionality to off the screen.
I have tried following codes
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
and wake locks
#Override
public void onReceive(Context context, Intent intent) {
if ((intent.getAction().equals(Intent.ACTION_SCREEN_OFF))) {
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "TEST");
wakeLock.acquire();
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent inten = new Intent(context,NewActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, inten, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 100, pi);
}
}
// Finish your WakeLock HERE. call this method after U put the activity in front or when u exit from the new activity.
public void finishWakeLocker(){
if (wakeLock != null)
wakeLock.release();
}
Thanks in advance
This is not possible, for security reasons. HOME and POWER buttons are two things you will not be able to directly override without system-grade permissions. After all, the user wouldn't like your app to take control of their device, would they? ;-)
You should plan the functionality in such a way that a wakelock or
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
is enough :-)
EDIT:
There are tutorials such as the one uday linked, but the effects are generally unreliable and work only on some devices. I have developed an app like that and trust me, you do not want to have to debug this ;-)
Related
I'm working on a thing where I need to override the power button in a Fragment when pressed. I did this successfully, however, the screen turns off when the power button is pressed.
I did a quick search and came across a few posts like this, this, and this. Some say it's doable and others say it's not. My question is that is this even doable? If so, any idea how to go about this?
You can use this to prevent screen from locking
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
While using in fragment
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Use a BroadcastReceiver and listen for the android.intent.action.SCREEN_OFF or Intent.ACTION_SCREEN_OFF which is sent when power button is pressed (and screen is on). What i think is that overriding the power button is not possible , because of security issues.
What you are doing is not advisable. However you can write a broadcast receiver and then the screen gets locked, wake it up using AlarmManager. Below is the code.
#Override
public void onReceive(Context context, Intent intent) {
if ((intent.getAction().equals(Intent.ACTION_SCREEN_OFF))) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "TEST");
wakeLock.acquire();
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent inten = new Intent(context,NewActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, inten, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 100, pi);
}
}
// Finish your WakeLock HERE. call this method after U put the activity in front or when u exit from the new activity.
public void finishWakeLocker(){
if (wakeLock != null)
wakeLock.release();
}
You cannot override the power button functionality however you can prevent system from showing dialog on long press of a power button using following.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(!hasFocus) {
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
I'm using a BroadcastReceiver that receives a broadcast from an AlarmManager. In the receiver I am starting two activities. One activity is started from a URI like this and is a third-party app:
// Open spotify
Intent spotify = new Intent(Intent.ACTION_VIEW, Uri.parse(song));
spotify.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(spotify);
} catch (ActivityNotFoundException e) {
status = Status.SPOTIFY_NOT_INSTALLED;
}
After that I start another activity that belongs to the app with a 5 second delay using the AlarmManager again:
public static void setExact(
Context context, PendingIntent pendingIntent, long time
) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
am.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
else
am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
public static void setExactDelay(
Context context, PendingIntent pendingIntent, long delay
) {
setExact(context, pendingIntent, System.currentTimeMillis() + delay);
}
PendingIntent pendingIntent = AlarmPlayActivity.makePendingIntent(context, alarm, status, startTime);
AlarmSet.setExactDelay(context, pendingIntent, 5000);
The second activity starts in 5 seconds as expected. However the first activity only starts when the device is unlocked. If the device is locked it does not start on Android Nougat (7.0). This is the case even when the lock is not secured by password, pattern etc. This used to work on earlier Android versions, even with a secure lock.
Is there a method by which I can start the first activity without needing the screen to be on?
Edit: I've tried using the following IntentService. It works when the device is awake and unlocked but there is no luck when the device is locked:
public class AlarmService extends IntentService {
static final int NOTIFICATION_ID = 1;
public AlarmService() {
super("AlarmService");
}
public static Intent makeIntent(Context context, Alarm alarm, AlarmReceiver.Status status, long startTime) {
Intent intent = IntentFactory.alarmPlayIntent(alarm, status, startTime);
intent.setClass(context, AlarmService.class);
return intent;
}
private static void sleep(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// Nothing
}
}
#Override
protected void onHandleIntent(Intent intent) {
// Acquire Wakelock immediately
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(
PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE,
"AlarmServiceWakeLock"
);
wakeLock.acquire();
KeyguardManager.KeyguardLock lock = ((KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
// Get intent data
final Alarm alarm = IntentFactory.getAlarm(intent);
AlarmReceiver.Status status = IntentFactory.getStatus(intent);
final long startTime = IntentFactory.getStartTime(intent, 0);
// Get a random song for this alarm
AlarmDatabase db = AlarmDatabase.getInstance(this);
Song song = db.getRandomSong(alarm);
String songName = song == null ? "backup sound" : song.getName();
// Start a foreground notification
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(getText(R.string.alarm_starting_notification_title))
.setContentText(getString(
R.string.alarm_starting_notification_message, alarm.getName(), songName
))
.setSmallIcon(R.drawable.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_MAX)
.build();
startForeground(NOTIFICATION_ID, notification);
// Potentially open Spotify if we can
if (song != null) {
// Open spotify
Intent spotify = new Intent(Intent.ACTION_VIEW, Uri.parse(song.getUri()));
spotify.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
try {
startActivity(spotify);
} catch (ActivityNotFoundException e) {
status = AlarmReceiver.Status.SPOTIFY_NOT_INSTALLED;
}
} else
status = AlarmReceiver.Status.NO_SONGS;
// Start play activity in 10 seconds, giving Spotify some chance to load up.
sleep(10);
startActivity(AlarmPlayActivity.makeIntent(this, alarm, status, startTime));
// Keep alive for 5 more seconds
sleep(5);
// Stop notification
stopForeground(true);
// Release wakelock
wakeLock.release();
}
}
I experienced the same issue and i think Intent.ACTION_VIEW don't work until you unlock the screen because of safety reasons.
Same issue discussed here also. You can also check this link
You have to use up-to-date methods:
either
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nexttime, pendingintent);
or
am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nexttime, pendingintent);
Do not forget to set your i awake mode:
to start:
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, A_.APPNAME + Integer.toString(index));
if (wakeLock != null && wakeLock.isHeld()){
wakeLock.acquire();
}
to release:
wakeLock.release();
you can set your activity as foreground (over lock screen)
described here:
https://stackoverflow.com/a/23611199/1979882
import android.view.Window;
import android.view.WindowManager.LayoutParams;
Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
You can start your activity as shown here too:
https://stackoverflow.com/a/6468575/1979882
#Override
public void onReceive(Context context, Intent intent) {
//start activity
Intent i = new Intent();
i.setClassName("com.test", "com.test.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
Instead of starting your Spotify activity directly from the receiver, have the receiver instead start a Service from your own app.
Inside of the service, acquire your wakelock and then start a Foreground notification (build notification with NotificationCompat.Builder then use startForeground(mId, notification);, this notification will keep the service alive in cases where Nougat would otherwise stop it.
Then fire your Spotify Intent from the service, afterwards of course kill the service, foreground notification, and wakelock. This has worked for me before to avoid pesky Nougat problems like you are facing. Hope this helps!
In my case, the second activity also could not open without wake your phone up.
Solution:
Start activity when screen is off
Bring app to front, turn on display and unlock from AlarmManager?
I have this problem with inconsistency of launching alarms while device is in sleep mode (well documented problem), that is driving me crazy.
In my app for frequent reminders i use alarmmanager to set time for next reminder, when the previous one occurs. In most cases app runs fine, but in sleep mode on my ASUS TF300t (API 17), the alarms goes bonkers.
Code for registering alarm:
Intent i = new Intent(mContext, OnAlarmReceiver.class);
i.putExtra(RReminder.PERIOD_TYPE, type);
i.putExtra(RReminder.EXTEND_COUNT, extendCount);
i.setAction(RReminder.CUSTOM_INTENT_ALARM_PERIOD_END);
pi = PendingIntent.getBroadcast(mContext, (int)when, i, PendingIntent.FLAG_ONE_SHOT);
if(buildNumber >= Build.VERSION_CODES.LOLLIPOP){
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(when, pi);
mAlarmManager.setAlarmClock(alarmClockInfo,pi);
} else if(buildNumber >= Build.VERSION_CODES.KITKAT){
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, when, pi);
} else {
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when, pi);
}
And my WakefulBroadcastReceiver class:
public class OnAlarmReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
int type = intent.getExtras().getInt(RReminder.PERIOD_TYPE);
int extendCount = intent.getExtras().getInt(RReminder.EXTEND_COUNT);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Log.d("period type",""+type);
Log.d("time",sdf.format(Calendar.getInstance().getTime()));
Intent i = new Intent(context, PeriodService.class);
i.putExtra(RReminder.PERIOD_TYPE, type);
i.putExtra(RReminder.EXTEND_COUNT, extendCount);
startWakefulService(context,i);
}
}
First thing i am observing is the alarm-batching, when an alarm isn't launched when it was supposed, but instead it launches when the next alarm is due together with the second one.
Other thing is, when an alarm does not launch on time in sleep mode, it goes off right after i wake up the device with power button.
I am aware of the changes of alarm behaviour in API 19 and 23 (doze mode). I solved the inexact issues with newer API's observed in emulated virtual devices with setExact().
I have two physical android devices, this TF300t tablet and Galaxy S3 phone. And while with S3 this problem is occurs very rarely, the consistency of problems on TF300t basically makes my app useless.
So my question would be, are my described problems present on many devices across all API's (which would require me to look for entireally different solution), or are they device or API specific and can be dealt as exception?
Use PowerManger to solve this:
Define in Service:
PowerManager powerManager;
PowerManager.WakeLock wakeLock;
Now in Service class, In Oncreate Metod add this:
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NAME");
In startCommnd method, add this before starting your Work:
wakeLock.acquire();
Add this in your Service class:
#Override
public void onDestroy() {
wakeLock.release();
this.isRun=false;
}
Finally, add in Manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Am developing a voip application. There is a background service which shows incoming call notifications, which works as expected (showing an incoming call dialog) when the phone is not locked and app is in background state.
How can i generate a dialog with interactive buttons, like whatsapp incoming call notification; even when the phone is locked?
Any heads up on this one or documentation that i can look up?
I can send an inapp notification for an incoming call, but that seems to be not enough for the purpose. I would need a full blow dialog interface which has a button or similar that would in turn open the application.
I am using Quickblox as the voip service provider.
Thanks in advance
I have tried unlocking the phone, on a button press
Here is my code to open a dialog from the background service.
viewToShowOnLockedScreen = View.inflate(getApplicationContext(), R.layout.activity_background_call, null);
viewToShowOnLockedScreen.setTag(TAG);
int top = getApplicationContext().getResources().getDisplayMetrics().heightPixels / 2;
final WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, Utils.dpToPx(300), 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON ,
PixelFormat.RGBA_8888);
viewToShowOnLockedScreen.setVisibility(View.VISIBLE);
Animation mAnimation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_slide_in_top);
viewToShowOnLockedScreen.startAnimation(mAnimation);
mWindowManager.addView(viewToShowOnLockedScreen, mLayoutParams);
mWindowManager.updateViewLayout(viewToShowOnLockedScreen, mLayoutParams);
And here is the code to unlock the device on a button press. Though this looks like it unlocks the phone, the screen is on, but the phone is still locked. Home button does'nt work.
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
Not really an answer, a work around i should say.
I coul'dnt unlock the screen. However I have updated the application to listen to Intent.ACTION_USER_PRESENT and added necessary to logic in the app to answer calls once the user has unlocked the device at the point of incoming call. Here is the code.
mUnlockStatusFilter = new IntentFilter();
mUnlockStatusFilter.addAction(Intent.ACTION_USER_PRESENT);
mUnlockStateIntentReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent i) {
if (i.getAction().equals(Intent.ACTION_USER_PRESENT)) {
//Do something phone is unlocked
Log.d(TAG,"Screen unlocked");
if(isWaitingForUnlock){
stopCallNotification();
if(Foreground.get().isForeground()){
Log.d(TAG, "App is in foreground; Sending a broadcast");
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setAction(BaseActivityWithSignalling.onReceiveNewSession);
intent.putExtra("code", BaseActivityWithSignalling.onReceiveNewSessionCode);
sendBroadcast(intent);
Log.d(TAG, "Send broadcast for onReceiveNewSession");
}else{
Log.d(TAG, "App is in background; Showing activity");
Intent showCallingFromBG = new Intent(LMService.this, BackgroundCall.class);
showCallingFromBG.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(showCallingFromBG);
}
}
}
}
};
registerReceiver(mUnlockStateIntentReceiver, mUnlockStatusFilter);
For testing purposes i have made a service that beeps
every 1 minute. (No client-server interface yet). It beeps okay when
the screen in on, but when it goes to sleep the beeping stops.
I am making an application that has to periodically poll the a server
for something.
For this, I am trying to create a service that'll constantly be
running in the background, poll the server every 1 min and then based
on the reply from server it shall generate a task bar notification.
I have a test activity with two buttons, 1 to start and the other to
stop the service. And one service class named S_PS_PollService
The setOnClickListener of 'Start Activity' button contains:
Thread pollServiceThread = new Thread() {
public void run() {
startService(new Intent(MM_MainMenu.this,
S_PS_PollService.class));
}
};
pollServiceThread.start();
The 'Stop Activity' button simply has:
stopService(new Intent(MM_MainMenu.this, S_PS_PollService.class));
Following are the methods from S_PS_PollService class:
public void onCreate() {
pollSound = MediaPlayer.create(this, R.raw.chirp);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(this, S_PS_PollService.class);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
// for wake lock
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag")
// for calendar
calendar = Calendar.getInstance();
}
Onstart:
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
wl.acquire();
pollSound.start();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MILLISECOND, 60000);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
wl.release();
}
Whenever the alarm kicks off onStart() method is executed, making the
beep and setting new alarm. But it works only as long as screen is on.
I have tried for https://github.com/commonsguy/cwac-wakeful but didnt
get it. Relatively new to android ...
Please help me, im very desperate :) Thanks, !
You have to use the AlarmManager, there are plenty of posts here on stackoverflow.
You want to acquire a partial wake lock (leaving the CPU running whenever sleep is entered on the device) as suggested by your code.
The issue is your presumably overriden on start releases the wake lock. You want to release your wakeLock in onDestroy .. once your service is finished running.
This finally worked for me.
Download the CWAC-WakefulIntentService.jar from https://github.com/commonsguy/cwac-wakeful
add a class in your project
import com.commonsware.cwac.wakeful.WakefulIntentService;
public class WakeService extends WakefulIntentService {
public WakeService(String name) {
super(name);
}
#Override
protected void doWakefulWork(Intent intent) {
}
}
now add the following line in your code where ever you want to repeat the loop and wake the device up
WakefulIntentService.sendWakefulWork(this, S_WS_WakeService.class);