Reputation: 45
I am using Alarm services to keep my application running when mobile is locked and screen turned off but it does not work.
My Alarm service class is:
public class AlarmService extends BroadcastReceiver {
// Restart service every 60 seconds
private static final long REPEAT_TIME = 1000 * 60;
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, StartUp_broadcast.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 60 seconds after boot completed
cal.add(Calendar.SECOND, 60);
//
// Fetch every 60 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setRepeating (AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), REPEAT_TIME, pending);
// service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
// REPEAT_TIME, pending);
}
}
and my brodcast service class is:
public class StartUp_broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.
getService(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + 1000, 1000, pendingIntent);
Intent Startup_Intent = new Intent(context, LocationService.class);
Startup_Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(Startup_Intent);
}
}
My main Activity class has PowerManager Service for WakeLock
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeUp");
wl.acquire(1000); //wake up the screen
setContentView(R.layout.main);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Kindly help me. How to keep my application running when mobile is locked and scrren is off ?
Upvotes: 0
Views: 2150
Reputation: 1685
If you want to run application when screen lock and also run alaram then you need to start service at boot. may be this link is helpfull to you. http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/
first read and do your self step by step. i hope it is helpfull to you.
Upvotes: 1
Reputation: 2285
I can suggest you one thing..when your app will start and or the service will start, may be you can try to unlock the keyguard.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
So when your app is running, it will unlock the key guard and keep the screen on. And when the service is stop, again lock the key guard.
Note: Don't use the PowerManager class. Because it will drain the battery very fast. I faced this problem.
Thanks.
Upvotes: 0
Reputation: 29642
Your code is fine, only you need to change your Alarm variables to the class level with static modifier like below,
public class AlarmService extends BroadcastReceiver
{
private static Intent myIntent;
private static AlarmManager service;
private static PendingIntent pendingIntent;
.
.
.
}
Upvotes: 0