Search code examples
javaandroidhandlerandroid-8.0-oreo

Running an Alarm Service once a day on Oreo


I've created a service which consult notes if they are in recicle bin, it have to run once a day but with handler I think it will drain too much battery, So I need you help How can I do a task once a day without drain battery or other thing that's not a handler ?

When the app is launched the NotesApplication is created and start the NotesService class, it should be executing all day but it will drain battery, so I need to execute once a day

NotesApplication

public class NotesApplication extends Application {

String TAG = "NotesApplication";
Intent intent;

@Override
public void onCreate() {
    // TODO: Implement this method
    super.onCreate();
    Log.d(TAG, "Application created");
    intent = new Intent(getApplicationContext(), NotesService.class);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        Log.d(TAG, "Foreground service started");
        getApplicationContext().startForegroundService(intent);
    }else if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
        Log.d(TAG, "Service started");
        getApplicationContext().startService(intent);
    }
}

Notes Service

public class NotesService extends Service {

ArrayList<Notes> listNotas;
String TAG = "NotesService";
SQLiteHelperConnection conn;

@Override
public void onCreate()
{
    // TODO: Implement this method
    super.onCreate();
    Log.d(TAG, "Notes service created");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        final NotificationManager mNotific= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence name = "Axco";
        String description = "Service";
        int importance = NotificationManager.IMPORTANCE_MIN;
        final String ChannelID="Service Channel";
        NotificationChannel mChannel = new NotificationChannel(ChannelID, name, importance);
        mChannel.setDescription(description); 
        mChannel.setLightColor(ThemeClass.getColor()); 
        mChannel.canShowBadge(); 
        mChannel.setShowBadge(true); 
        mNotific.createNotificationChannel(mChannel);
        final int code = 101; 
        String body= "Service Running";
        Notification notification = new Notification.Builder(this, ChannelID)
            .setContentTitle(getPackageName())
            .setContentText(body)
            .setBadgeIconType(R.drawable.ic_launcher)
            .setNumber(1)
            .setSmallIcon(R.drawable.ic_launcher)
            .setAutoCancel(true)
            .build();
        startForeground(code, notification);
    }
    conn = new SQLiteHelperConnection(this, "db_notas.db", null, 1);
    listNotas = new ArrayList<Notes>();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    // TODO: Implement this method
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        final NotificationManager mNotific= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence name = "Axco";
        String description = "Service";
        int importance = NotificationManager.IMPORTANCE_MIN;
        final String ChannelID="Service Channel";
        NotificationChannel mChannel = new NotificationChannel(ChannelID, name, importance);
        mChannel.setDescription(description); 
        mChannel.setLightColor(ThemeClass.getColor()); 
        mChannel.canShowBadge(); 
        mChannel.setShowBadge(true); 
        mNotific.createNotificationChannel(mChannel);
        final int code = 101; 
        String body= "Service Running";
        Notification notification = new Notification.Builder(this, ChannelID)
            .setContentTitle(getPackageName())
            .setContentText(body)
            .setBadgeIconType(R.drawable.ic_launcher)
            .setNumber(1)
            .setSmallIcon(R.drawable.ic_launcher)
            .setAutoCancel(true)
            .build();
        startForeground(code, notification);
    }
    final Handler handler = new Handler();
    Runnable runnable = new Runnable(){
        @Override
        public void run()
        {
            // TODO: Implement this method
            Log.d(TAG, "Service running");
            listNotas.clear();
            consult();
            check();
            handler.postDelayed(this, 15000);
        }
    };
    handler.post(runnable);
    return Service.START_STICKY;
}

private void consult(){
    Log.d(TAG, "Consulting...");
    SQLiteDatabase db = conn.getReadableDatabase();

    Notes notas = null;

    Cursor cursor = db.rawQuery("SELECT * FROM "+Utilities.TABLA_NOTA, null);

    while (cursor.moveToNext()) {
        notas = new Notes();
        notas.setId(cursor.getString(0));
        notas.setLastModified(cursor.getString(5));
        notas.setLastModifiedDate(cursor.getString(7));

        boolean a = Boolean.valueOf(cursor.getString(4));

        if(a){
            listNotas.add(notas);
        }
    }
}

private void check(){
    //Do something
}

private void deleteNote(int position){
    SQLiteDatabase db = conn.getWritableDatabase();
    String[] parametros = {listNotas.get(position).getId()};
    db.delete(Utilities.TABLA_NOTA, Utilities.ID+"=?", parametros);
    listNotas.remove(position);
}

@Override
public IBinder onBind(Intent p1){
    // TODO: Implement this method
    return null;
}

Solution

  • Using Alarm Manager is efficient. See implementation

    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    
    Intent i= new Intent(context, AlarmManagerBroadcastReceiver.class);
    
    //intent.putExtra(something you want to put);
    
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    
    Calendar calendar = Calendar.getInstance();
    
    calendar.setTimeInMillis(System.currentTimeMillis());
    
    // check if it is more than 11 am. if so set alarm for next day
    
    if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY)) {
        calendar.add(Calendar.DAY_OF_YEAR, 1);
    }
    
    // everyday at 11 am
    calendar.set(Calendar.HOUR_OF_DAY, 9);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    
    
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pi);
    // alarm set
    

    Finally create a broadcast receiver to do the work you want.