我的应用程序有3个通知,无论如何时间更改>>?

时间:2016-09-15 12:01:27

标签: android notifications broadcastreceiver alarmmanager

嗨我需要一种方法来改变通知之间的时间因为所有三个结合在一起2 ----我想要一些事情,比如每5分钟循环一次,如果有可能的话就添加新的通知。 (日历是评论我尝试过这种方式既不起作用)

<% Dim top As Integer = -425
Dim marginTop As String = "margin-top:" + top.ToString() + "px"
%>

<ul style='<%: marginTop %>'">

}

Reciever.java

public class MainActivity extends AppCompatActivity {


Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.btn1);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar calendar = Calendar.getInstance();
           /* calendar.set(Calendar.HOUR_OF_DAY, 2);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 0);
            Calendar calendar2 =Calendar.getInstance();
            calendar2.set(Calendar.HOUR_OF_DAY, 2);
            calendar2.set(Calendar.MINUTE, 59);
            calendar2.set(Calendar.SECOND, 10);


            Calendar calendar3 =Calendar.getInstance();
            calendar3.set(Calendar.HOUR_OF_DAY, 2);
            calendar3.set(Calendar.MINUTE, 59);
            calendar3.set(Calendar.SECOND, 15);*/

            Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);
    //             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
            AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES/90 , pendingIntent);
            alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES , pendingIntent1);
            alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES , pendingIntent2);

        }
    });

}


protected void onStart() {

    Intent intent = new Intent(this,AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(),101,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(),99,intent,PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    alarmManager.cancel(pendingIntent1);
    alarmManager.cancel(pendingIntent2);
    super.onStart();
}


@Override
protected void onPause() {

    Intent intent = new Intent(this,AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(),101,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(),99,intent,PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    alarmManager.cancel(pendingIntent1);
    alarmManager.cancel(pendingIntent2);
    super.onPause();
}

@Override
protected void onStop() {

    Calendar calendar = Calendar.getInstance();
    Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);
   // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
    AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES , pendingIntent);
    alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES , pendingIntent1);
    alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES , pendingIntent2);

    super.onStop();
}

@Override
protected void onDestroy() {
    Calendar calendar = Calendar.getInstance();
    Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);
 //   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
    AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES , pendingIntent);
    alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES , pendingIntent1);
    alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES , pendingIntent2);

    super.onDestroy();
}

以及点击通知Activity12.class

时的活动
public class AlertReceiver extends BroadcastReceiver {



@Override
public void onReceive(final Context context, Intent intent) {
     NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    final Intent repeating_intent = new Intent(context,Activity12.class);
//    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
  NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("AAA")
            .setContentText("BBB");
          builder.setAutoCancel(true);
/*    NotificationCompat.InboxStyle inboxStyle = new 
        notificationManager.notify(100,builder.build());
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            PendingIntent pendingIntent1 = PendingIntent.getActivity(context,101,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder builder1 = new NotificationCompat.Builder(context).setContentIntent(pendingIntent1)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("CCC")
                    .setContentText("DDD");
            builder1.setAutoCancel(true);
            NotificationManager notificationManager1 = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager1.notify(101,builder1.build());            }
    },1*60*1000);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            PendingIntent pendingIntent2 = PendingIntent.getActivity(context,99,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder builder2 = new NotificationCompat.Builder(context).setContentIntent(pendingIntent2)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("EEE")
                    .setContentText("FFF");
            builder2.setAutoCancel(true);

            NotificationManager notificationManager2 = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager2.notify(99,builder2.build());
        }
    },2*60*1000);

}

1 个答案:

答案 0 :(得分:0)

您可以使用处理程序在生成通知时延迟一些

//Create First Notification
// Time delay 0 min.

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
             // Time delay 5 min.
            //Create second notification        
        }
    },5*60*1000);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
           // Time delay 10 min.
             //create 3rd notification
        }
    },10*60*1000);