Reputation: 11
I am creating a privacy task calendar. It uses an alarm manager and notifications. When the application is closed, the alarm service is apparently running and causing notifications. The notification has two actions - "set as completed" and "cancel". When I click "set as completed" in my task manager, there is nothing in the task list, because memory was freed, because the application is closed. A null pointer exception occurs.
Is it normal to use a constantly running service to store data, where you can set a task as completed?
Am I correct in understanding that WhatsApp uses a constantly running service with incoming message listener?
P.S. Sorry for my bad English. It's Google translate.
I cannot use the database, because it is encrypted. Only when I open the application, it is decrypted, and the task list is loaded into RAM.
I cannot use shared preferences, because there is a possibility of an information leak when connecting to a PC.
UPD:
TaskManager
public Task getTaskById(int id) {
Log.d("TaskCalendarLog", "Task manager getByID: task id = " + id);
int index = 0;
Log.d("TaskCalendarLog", "Task list = " + taskList);
for (Task task:taskList) {// <------- null pointer exception in taskList
Log.d("TaskCalendarLog", "Task manager for: id = " + id);
if (task.getId() == id) {
return taskList.get(index);
}
index++;
}
return null;
}
TaskAlarmReciever
Intent setDoneIntent = new Intent(context, ActionSetTaskDoneBroadcastReceiver.class);
setDoneIntent.putExtra("id", taskId);
PendingIntent setDonePendingIntent = PendingIntent.getBroadcast(context, 0, setDoneIntent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Action cancel = new NotificationCompat.Action(R.drawable.ic_cancel, "Cancel", cancelPendingIntent);
NotificationCompat.Action setDone = new NotificationCompat.Action(R.drawable.ic_done, "Set done", setDonePendingIntent);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "TaskNotificationChannel")
.setSmallIcon(R.drawable.ic_alarm)
.setContentTitle("Task is now")
.setContentText(taskText)
.setGroup(GROUP_KEY_ALARM)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setFullScreenIntent(taskAlarmPendingIntent, true)
.setChannelId("TaskAlarmChannel")
.addAction(cancel)
.addAction(setDone)
.setVibrate(new long[] {100, 100, 200, 200, 300, 300, 400, 400, 500, 500, 400, 400, 300, 300, 200, 200, 100})
.setAllowSystemGeneratedContextualActions(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(taskText))
.setCategory(NotificationCompat.CATEGORY_CALL)
.setAutoCancel(true);
ActionSetTaskDoneBroadcastReciever
public void onReceive(Context context, Intent intent) {
int id = intent.getIntExtra("id", 0);
TaskManager taskManager = TaskManager.getInstance();
Log.d("TaskCalendarLog", "Action: task id = " + id);
taskManager.getTaskById(id).setIsDone("true");
TaskApplication.getInstance().restartAllTaskAlarm();
Intent taskRintoneServiceIntent = new Intent(context, RingtoneService.class);
context.stopService(taskRintoneServiceIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Log.d("TaskCalendarLog", "Notification id: " + id);
notificationManager.cancel(id);
}
Upvotes: 1
Views: 88