运行 奥利奥每天一次的警报服务

Running an Alarm Service once a day on Oreo

我创建了一项服务,如果它们在 recicle bin 中,它会查询注释,它必须每天 运行 一次,但是使用 handler 我认为它会耗尽太多电池,所以我需要你的帮助如何我可以每天做一次任务而不用耗尽电池或其他不是处理程序的东西吗?

启动应用程序时,会创建 NotesApplication 并启动 NotesService class,它应该全天执行但会耗尽电池电量,所以我需要每天执行一次

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);
    }
}

笔记服务

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;
}

使用警报管理器是高效的。 查看实现

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

最后创建一个广播接收器来完成你想要的工作。