Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been developing an android application in which there is a service and I want to run it for ever.

I am using a broadcast receiver to get screen on-off intents inside oncreate of service and using an alarm manager which starts the service itself after every 25 seconds inside onstartcommand and also performing my other operations in onstartcommand. I have put all the code in onstartcommand inside an thread. But the problem is once the service in running for some hours in background and I reopen the app the app starts lagging and becomes very slow. Before starting the service the app works very fine.

My questions are -

How can I optimize the service as to run it for ever in background without causing the lag in app
Is there any way to get screen on off intents by system or anything else instead of running the service with broadcast receiver

What I have tried:

public class MyService extends Service {

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

AlarmManager alarmManagerMain;
BroadcastReceiver receiver;
Thread t;
Thread thread;

@Override
public void onCreate() {
super.onCreate();
t = new Thread(() -> {
HandleReceiver();
});
t.start();
CreateNotificationChannel();
startNotification();
}

private void HandleReceiver() {
try {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
// ...
// My Code
// ...
// Starting Service to call onStartCommand
Intent serviceIntent = new Intent(context, MyService.class);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
startForegroundService(serviceIntent);
}
else
{
startService(serviceIntent);
}
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
// ...
// My Code
// ...
}
}
};
MyService.this.registerReceiver(receiver, intentFilter);
}
catch (Exception ignore) { }
}

@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
Runnable runnable = () -> {
if(allowNotificationAndAlerts)
{
// My Code
}
// This function is to start the service even after the system kills it
startAlarm();
};
thread = new Thread(runnable);
thread.start();
return super.onStartCommand(intent, flags, startId);
}

private void startAlarm()
{
AlarmManager alarmManager = (AlarmManager) MyService.this.getSystemService(Service.ALARM_SERVICE);
Intent myIntent = new Intent(MyService.this, MyBroadCastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyService.this, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 25);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}

@Override
public void onDestroy() {
stopForeground(false);
unregisterReceiver(receiver);
super.onDestroy();
}
}
Posted
Comments
[no name] 12-Jun-21 12:10pm    
A memory leak or CPU bound. Or both.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900