运行任务时,显示通知
问题描述:
(对不起我的英语)运行任务时,显示通知
我需要用我的第一个程序,我必须作出周五通过对研究过程中帮助...
我想创建一个应用程序,该应用程序将显示带添加提醒的表格中的描述的通知。我在获取下一个描述的机制方面遇到问题。显示通知后,我对应用程序进行了描述。不幸的是,这在应用程序关闭时不起作用。
public void onReceive(Context context, Intent intent) {
ScheduleNotification.completeWakefulIntent(intent);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
descToShow = d1;
DataBaseHandler dbh = new DataBaseHandler(context);
dbh.open();
dbh.DeleteRow(descToShow);
dbh.close();
Toast.makeText(context, descToShow, Toast.LENGTH_LONG).show();
//notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.icon);
builder.setContentTitle(descToShow);
builder.setContentText(descToShow); //--description event--
builder.setAutoCancel(false).build();
//from android dev:
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, builder.build());
PendingIntent notifyPIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
builder.setContentIntent(notifyPIntent);
}
任何想法?
我github上的所有代码:https://github.com/azernax/dForget
答
的问题是,你用从你MainActivity
静态字段时MainActivity
不在内存中(所以你得到的默认值,而不是那些onCreate()
设置)。正确的解决方案是再次加载数据(在你的情况下从SharedPreferences
)。
我希望这可以帮助你。如果您有任何问题,请留言
不要为每个值使用单独的'SharedPreferences' - 可以容纳所有值。相关值应存储在一个文件中 – Markaos