如何为这个通知投入时间..!

时间:2016-09-17 07:55:20

标签: android notifications

嘿,我从https://developer.android.com/training/notify-user/managing.html得到了这个例子 但是没有做什么改变我没有更新我为了功能而放了50个通知,但是所有50个一起出现在一起我怎样才能让每一个出现在1或5分钟内 刚试过的处理程序没有工作看看我的代码plz

MainActivity.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) {

            final Calendar calendar = Calendar.getInstance();


            Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);

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

            final AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        //    final AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
       //     final 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);

        }
    });

}

AlertReciever.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")
            .setOngoing(false)

            .setContentText("BBB");
    builder.setAutoCancel(true);


    notificationManager.notify(100, builder.build());


    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            for (int a = 0; a < 5; a++) {
                PendingIntent pendingIntent1 = PendingIntent.getActivity(context, a, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
                NotificationCompat.Builder builder1 = new NotificationCompat.Builder(context).setContentIntent(pendingIntent1)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("CCC")
                        .setContentText("DDD")
                        .setOngoing(false)
                        .setAutoCancel(true);

                int numMessages = 0;

                builder1.setContentText("addsadasdadad")
                        .setNumber(++numMessages);

                builder1.setAutoCancel(true);
                NotificationManager notificationManager1 = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

                notificationManager1.notify(a, builder1.build());

            }
        }
    }, 1 * 60 * 1000);

2 个答案:

答案 0 :(得分:0)

以下代码可以帮助您。

Handler mHandler=new Handler;
mHandler.postDelayed(mRunnable, 60000); 

Runnable mRunnable=new Runnable()
{
    @Override 
    public void run()
    { 
       // your notification code
       mHnadler.postDelayed(mRunnable ,60000);

    }
} 

答案 1 :(得分:0)

//使用此方法设置alaram(根据需要设置时间)

 public static void setAlarm(Context context, String action, int actionCode, long syncTime)
    {
        AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(action);
        intent.putExtra(ACTION, actionCode);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context, actionCode + 1000, intent, 0);
        alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + (interval), interval, alarmIntent);
    }

//放置intent reciver

public class AlarmIntentReceiver extends BroadcastReceiver
{

    public static final int ACTION_NAME= 101;


    @Override
    public void onReceive(Context context, Intent intent)
    {
        int action = intent.getIntExtra(AlarmUtil.ACTION, 0);
        switch(action)
        {
            case ACTION_XYZ:
//perform action according to need          
 break;
        }
    }
}