2日历显示1通知

时间:2016-04-13 19:22:31

标签: android android-studio calendar alarmmanager alarm

我有一个应用程序工作,用时间表提醒用户

我将一个日历放在新课程中并附上通知并转到主要活动并调用它。

但是第一种方法只显示了通知,但第二种方法没有显示为什么?????????

MainActivity.java

        package com.osman.calendar;

    import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.pushbots.push.Pushbots;

    public class MainActivity extends AppCompatActivity {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Pushbots.sharedInstance().init(this);

            new section(this,1, 18, 30,"Title","new message","new message");
            new section(this,2, 18, 37,"Title","get new","get new");

        }
    }

section.java

package com.osman.calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import java.util.Calendar;

public class section {

    public section(Context context,int id,int Hours,int Minute, String Tilte , String Text, String Alert){
        AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra("id",id);
        intent.putExtra("Title",Tilte);
        intent.putExtra("Text",Text);
        intent.putExtra("Alert",Alert);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, Hours);
        calendar.set(Calendar.MINUTE, Minute);

        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                1000 * 60 * 60 * 24 * 7, alarmIntent);


    }
}

AlarmReceiver.java

        package com.osman.calendar;

    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Vibrator;
    import android.support.v4.app.NotificationCompat;
    import android.widget.Toast;


    public class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Time is up!!!!.",Toast.LENGTH_LONG).show();
            String Title = intent.getExtras().getString("Title");
            String Text = intent.getExtras().getString("Text");
            String Alert = intent.getExtras().getString("Alert");
            int id = intent.getExtras().getInt("id");
            createNotification(context, Title, Text, Alert,id);
            // Vibrate the mobile phone
            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(2000);
        }

        public void createNotification (Context context, String msg,String msgText, String msgAlert,int id){

            PendingIntent  notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(msg)
                    .setContentText(msgText)
                    .setTicker(msgAlert);
            mBuilder.setContentIntent(notificIntent);
            mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
            mBuilder.setAutoCancel(true);
            NotificationManager mNotificationMAnger =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationMAnger.notify(id, mBuilder.build());
        }

    }

1 个答案:

答案 0 :(得分:0)

如果您使用与之前相同的PendingIntent安排闹钟,则会更新之前设置的闹钟,而不会设置新闹钟。只有一个警报被触发,因为你第二次使用它时PendingIntent没有改变,只有额外内容发生变化(根据this问题的回答,这不是一个改变)

要更新旧警报但设置新警报,您必须使用不同的请求代码(getBroadcast()中的第二个参数 - 当前您对两个警报都使用0)。 只需使用您在section-constructor中作为参数的id作为request-code。