使用向上工具栏按钮android通知循环

时间:2016-04-19 08:05:12

标签: java android loops notify up-button

首先抱歉我可怕的英语,这是我的问题:

我有一个MainActivity.class设置alarmmanager,在特定时间我的alarmReceiver检查数据库是否有特定记录。如果检查结果为false,则启动显示我的通知的服务。

通知打开我的MealActivity.class,让用户可以在数据库中插入记录(与alarmReceiver的检查相同)并设置新的警报(至少+5小时)。

问题开始时,在我的MealActivity.class上,没有插入记录,我点击工具栏上的“向上按钮”以支持父活动(MainActivity.class),在这里,在MainActivity中,我收到一个新的通知,和以前一样。但是为什么不应该因为警报的下一个通知已经设置为至少五个小时,超过几秒钟。

为什么呢?

在我的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..
 }

My AlarmReceiver:

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();


   }
}

MealActivity:

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...
}
}

感谢您的帮助。

0 个答案:

没有答案