まず第一に、私のひどい英語で申し訳ありません、これは私の問題です:
私はアラームマネージャーを設定するMainActivity.classを持っています。特定の時間に、特定のレコードがあるかどうかをデータベースでalarmReceiverチェックします。チェック結果が偽の場合、彼は私の通知を表示するサービスを開始します。
通知は MealActivity.class を開いて、ユーザーがデータベースにレコードを挿入し (alarmReceiver のチェックと同じ)、新しいアラームを設定できるようにします (少なくとも +5 時間)。
問題は、私の MealActivity.class で、レコードを挿入せずに、ツールバーの「上へ」ボタンをクリックして父親のアクティビティ (MainActivity.class) に戻ると始まります。以前。しかし、アラームの次の通知が少なくとも 5 時間後 (数秒以上) に設定されているため、そうすべきではありません。
なんで?
私の MealActivity では、「上へ」ボタンを押すと、通知なしで MainActivity に戻りたいと思っています。次の通知は、指定された時間に表示されます。
どうすればこれを解決できますか?
MainActivity.class:
public class MainActivity extends AppCompatActivity {
private AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/**
* Set alarm for food
*/
Calendar calendar = new GregorianCalendar();
int mealType;
if((calendar.get(Calendar.HOUR_OF_DAY) >= 0 && calendar.get(Calendar.HOUR_OF_DAY) < 11) || (calendar.get(Calendar.HOUR_OF_DAY) >= 23 && calendar.get(Calendar.HOUR_OF_DAY) < 24)){
mealType = 0;
}
else if(calendar.get(Calendar.HOUR_OF_DAY) >= 11 && calendar.get(Calendar.HOUR_OF_DAY) < 16){
mealType = 1;
}
else mealType = 2;
setAlarm(mealType);
}
public void setAlarm(int mealType){
/**
* Set standard alarm.
*/
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(MainActivity.this, AlarmReceiver.class);
i.putExtra("mealType", mealType);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, i, PendingIntent.FLAG_ONE_SHOT);
Calendar c = new GregorianCalendar();
if(mealType == 0 && c.get(Calendar.HOUR_OF_DAY) >= 23) {
c.add(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.HOUR_OF_DAY, 11);
}
else if(mealType == 0 && c.get(Calendar.HOUR_OF_DAY) < 11) {
c.set(Calendar.HOUR_OF_DAY, 11);
}
else if(mealType == 1){
c.set(Calendar.HOUR_OF_DAY, 16);
}
else c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
}
// other stuff..
}
私のアラームレシーバー:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("ALARM RECEIVER", "Allarm received");
int mealType = intent.getExtras().getInt("mealType");
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(context);
databaseAccess.open();
int userID = databaseAccess.getActualID();
boolean thereIsAMeal = databaseAccess.thereIsAMeal(userID);
databaseAccess.close();
Intent service1 = new Intent(context, AlarmService.class);
if(!thereIsAMeal) {
Log.d("ALARM RECEIVER", "Start service");
service1.putExtra("mealType", mealType);
context.startService(service1);
}
}
}
私のアラームサービス:
public class AlarmService extends IntentService {
private static final int NOTIFICATION_ID = 1;
private static final String TAG = "MEALALARM";
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
public AlarmService() {
super("AlarmService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent,flags,startId);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "Alarm Service has started.");
Context context = this.getApplicationContext();
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
int mealType = intent.getExtras().getInt("mealType");
Intent mIntent = new Intent(this, MealActivity.class);
mIntent.putExtra("origin", "notify");
mIntent.putExtra("mealType", mealType);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_ONE_SHOT);
Resources res = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.icon))
.setTicker(res.getString(R.string.notification_title))
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.notification_title))
.setContentText(res.getString(R.string.notification_subject_food))
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Log.i(TAG, "Notifications sent.");
stopSelf();
}
}
食事内容:
public class MealActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
Intent intent = getIntent();
if (intent.getExtras().getString("origin").compareTo("notify") == 0) {
int mealType = intent.getExtras().getInt("mealType");
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
int actualID = databaseAccess.getActualID();
databaseAccess.close();
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(MealActivity.this, AlarmReceiver.class);
mealType = nextMeal(mealType);
i.putExtra("mealType", mealType);
PendingIntent pi = PendingIntent.getBroadcast(MealActivity.this, 0, i, PendingIntent.FLAG_ONE_SHOT);
Calendar c = Calendar.getInstance();
if(mealType == 0 && c.get(Calendar.HOUR_OF_DAY) >= 23) {
c.add(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.HOUR_OF_DAY, 11);
}
else if(mealType == 0 && c.get(Calendar.HOUR_OF_DAY) < 11) {
c.set(Calendar.HOUR_OF_DAY, 11);
}
else if(mealType == 1){
c.set(Calendar.HOUR_OF_DAY, 16);
}
else c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
}
} catch (NullPointerException ex){
}
// other stuff...
}
}
ご協力いただきありがとうございます。