我有一个应用程序使用AlarmManage
,Notification
,BroadcastService
在一段时间后通知用户。名称,性别,铃声,振动等值可在SharedPreferences
中进行编辑。
问题:
当我更改名称的值,性别,...值在首选项中更新但通知不更新值。但是,当我重新启动设备时,值是正确的,但只是首次运行。 :(
这是源代码
AlarmReceiver.class
public class AlarmReceiver extends BroadcastReceiver {
String TAG = getClass().getSimpleName();
String name = "", ringtone = "";
int gender, mZodiacID;
SharedPreferences shared;
@Override
public void onReceive(Context context, Intent intent) {
shared = PreferenceManager.getDefaultSharedPreferences(context);
showNotification(shared, context);
}
private void showNotification(SharedPreferences shared, Context context) {
Log.d(TAG, gender + " : " + name);
gender = Integer.parseInt(shared.getString(SHARED_GENERAL_GENDER, "0"));
name = shared.getString(SHARED_GENERAL_NAME, "");
Log.d(TAG, gender + " : " + name);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText("Xin chào " + name)
.setAutoCancel(true);
// Create explicit intent for an Activity
Intent notifyIntent = new Intent(context, AppDetailActivity.class);
notifyIntent.putExtra(PARAM_NAME, name);
notifyIntent.putExtra(PARAM_GENDER_ID, gender);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(AppDetailActivity.class);
stackBuilder.addNextIntent(notifyIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
mBuilder.setContentIntent(resultPendingIntent);
// Set ringtone for notification
Uri alarmSound;
ringtone = shared.getString(SHARED_NOTI_RINGTONE, "false");
if (ringtone.equals("false")) {
Log.d(TAG, "Default ringtone: " + ringtone);
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
} else {
Log.d(TAG, "Custom ringtone: " + ringtone);
alarmSound = Uri.parse(ringtone);
}
mBuilder.setSound(alarmSound);
// Set vibrate
if (shared.getBoolean(SHARED_NOTI_VIBRATE, false)) {
mBuilder.setVibrate(new long[]{0, 100, 200, 300});
Log.d(TAG, "Vibrate: YES");
} else {
mBuilder.setVibrate(new long[]{0, 0, 0, 0});
Log.d(TAG, "Vibrate: NO");
}
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
AlarmService.java
public class AlarmService {
private PendingIntent pendingIntent;
private Context mContext;
public AlarmService(Context context) {
mContext = context;
pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, AlarmReceiver.class), 0);
}
public void cancel() {
if (mContext != null) {
Object localObject = new Intent(mContext, AlarmReceiver.class);
localObject = PendingIntent.getBroadcast(this.mContext, 0, (Intent)localObject, 0);
((AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE)).cancel((PendingIntent)localObject);
Toast.makeText(mContext, "Đã tắt thông báo hàng ngày", Toast.LENGTH_SHORT).show();
}else{
// Toast.makeText(mContext, "Alarm can't cancel", Toast.LENGTH_SHORT).show();
}
}
public void start() {
AlarmManager manager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(mContext);
shared.edit().putBoolean(SHARED_NOTI_DAILY, true).commit();
Toast.makeText(mContext, "Đã bật thông báo hàng ngày", Toast.LENGTH_SHORT).show();
int interval = 1000 * 60 * 1;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int hous = shared.getInt(SHARED_NOTI_HOURSE, 7);
int minute = shared.getInt(SHARED_NOTI_MINUTES, 0);
calendar.set(Calendar.HOUR_OF_DAY, hous);
calendar.set(Calendar.MINUTE, minute);
// manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
// interval, pendingIntent);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval,pendingIntent);
}
}
答案 0 :(得分:0)
您可以添加一个监听器并更新您的通知。
SharedPreferences.OnSharedPreferenceChangeListener spChanged = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// your stuff here
}
};