java—使用alarmmanager实现ram内存的高利用率

x6492ojm  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(286)

我正在开发一个android应用程序,该软件每天都要提醒用户做一些事情。为了开发提醒,我使用了闹钟管理器,并设置每天的重复。提醒是在用户首次登录时设置的。但是我注意到这占用了大量的ram内存,其实以前使用的ram是60/80mb,现在我已经实现了提醒它是160mb,这就导致了设备上的滞后。使用ontimeset方法设置提醒。我怎样才能解决这个问题?也许高ram使用率可能是由通知图标的png图像造成的?
以下是提醒代码:

onTimeSet(22, 51);

    public void onTimeSet(int hourOfDay, int minute) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.HOUR_OF_DAY, hourOfDay);
            c.set(Calendar.MINUTE, minute);
            c.set(Calendar.SECOND, 0);
            startAlarm(c);
        }
        private void startAlarm(Calendar c) {

            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(this, AlertReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);

            if (c.before(Calendar.getInstance())) {
                c.add(Calendar.DATE, 1);
            }
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
                    AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
        }
        private void cancelAlarm() {
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(this, AlertReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
            alarmManager.cancel(pendingIntent);
        }

下面是通知帮助程序的代码:

public class NotificationHelper extends ContextWrapper {
public static final String channelID = "channelID";
public static final String channelName = "Channel Name";
private NotificationManager mManager;
public NotificationHelper(Context base) {
    super(base);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createChannel();
    }
}
@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {
    NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
    getManager().createNotificationChannel(channel);
}
public NotificationManager getManager() {
    if (mManager == null) {
        mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }
    return mManager;
}
public NotificationCompat.Builder getChannelNotification() {
    return new NotificationCompat.Builder(getApplicationContext(), channelID)
            .setContentTitle("Title")
            .setContentText("Do something")
            .setSmallIcon(R.drawable.app_icon_square);
}

}
alertreceiver的代码如下:

public class AlertReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationHelper notificationHelper = new NotificationHelper(context);
        NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
        notificationHelper.getManager().notify(1, nb.build());
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题