我想设置一个通知,它会在我收到通知时响铃

时间:2017-03-03 10:28:50

标签: java android alarmmanager alarm

  • 我正在创建一个学校应用程序,当我访问那里时 营销我每次都要提交一份报告。如果访问是 如果访问不成功那么成功则没有问题 必须重新安排我下次访问的会议。当我设定日期和 我下次会议的时间应该在清晨提醒我 发送通知,它应该在那时发出警报,这样我才能得到 提醒。这是我的报告活动代码请帮帮我

    private void sendNotificationDetailsTwo() {
    
        /* Calendar firingCal = Calendar.getInstance();
        Calendar currentCal = Calendar.getInstance();
    
        firingCal.set(Calendar.HOUR_OF_DAY, 8); // At the hour you wanna fire
        firingCal.set(Calendar.MINUTE, 30); // Particular minute
        firingCal.set(Calendar.SECOND, 0); // particular second
        Long intendedTime = firingCal.getTimeInMillis();
        if (firingCal.compareTo(currentCal) < 0) {
            firingCal.add(Calendar.DAY_OF_MONTH, 1);
        } else {
            for (int i = 0; i < 10; ++i) {
                Intent newIntentTwo = new Intent(Report_Activity.this, MyReceiver.class);
                final int intent_id_two = (int) System.currentTimeMillis();
                newIntentTwo.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                Bundle bundle = new Bundle();
                bundle.putString("date", str_view_date);
                bundle.putString("time", str_view_time);
                bundle.putString("school_name", str_show_school_name);
                bundle.putInt("intent_id", intent_id_two);
                newIntentTwo.putExtras(bundle);
                AlarmManager alarmManager = (AlarmManager)            Report_Activity.this.getSystemService(Report_Activity.this.ALARM_SERVICE);
    
                PendingIntent pendingIntent =    PendingIntent.getBroadcast(Report_Activity.this, intent_id_two, newIntentTwo,    PendingIntent.FLAG_ONE_SHOT);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
            }
        }*/
    
        /*alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);     alarmIntent = new Intent(context of current file, AlarmReceiver1.class); // AlarmReceiver1 = broadcast receiver
    
        pendingIntent = PendingIntent.getBroadcast(  Menu.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
        alarmManager.cancel(pendingIntent);
    
        Calendar alarmStartTime = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        alarmStartTime.set(Calendar.HOUR_OF_DAY, 8);
        alarmStartTime.set(Calendar.MINUTE, 00);
        alarmStartTime.set(Calendar.SECOND, 0);
        if (now.after(alarmStartTime)) {
            Log.d("Hey","Added a day");
            alarmStartTime.add(Calendar.DATE, 1);
        }
    
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        Log.d("Alarm","Alarms set for everyday 8 am.");*/
    
        Calendar firingCal = Calendar.getInstance();
        Calendar now       = Calendar.getInstance();
        firingCal.set(Calendar.HOUR_OF_DAY,15);
        firingCal.set(Calendar.MINUTE,25);
        firingCal.set(Calendar.SECOND,0);
        while(now.getTimeInMillis()>firingCal.getTimeInMillis()){
            firingCal.add(Calendar.DATE,1);
        }
        Intent myInten = new Intent(Report_Activity.this,MyReceiver.class);
        final int intent_id_two = (int) System.currentTimeMillis();
        myInten.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        Bundle bundle = new Bundle();
        bundle.putString("date", str_view_date);
        bundle.putString("time", str_view_time);
        bundle.putString("school_name", str_show_school_name);
        bundle.putInt("intent_id", intent_id_two);
        myInten.putExtras(bundle);
    
        //final int intent_id_two = (int) System.currentTimeMillis();
        PendingIntent myPendingIntent = PendingIntent.getBroadcast(Report_Activity.this,intent_id_two,myInten,PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager)Report_Activity.this.getSystemService(Report_Activity.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,firingCal.getTimeInMillis(),2000,myPendingIntent);
    
    
    }
    

    MyAlarmService.java

    public class MyAlarmService extends Service {
    
        private NotificationManager notificationManager;
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
          boolean alarm =(PendingIntent.getBroadcast(this,0,new Intent("Alarm"),PendingIntent.FLAG_NO_CREATE)==null);
    
    
            if(alarm){
                Intent mintent =new Intent("Alarm");
                PendingIntent mpendingIntent = PendingIntent.getBroadcast(this,0,mintent,0);
                Calendar mcalender = Calendar.getInstance();
                mcalender.setTimeInMillis(System.currentTimeMillis());
                mcalender.add(Calendar.SECOND,3);
                AlarmManager malarmmanager = (AlarmManager)getSystemService(ALARM_SERVICE);
                malarmmanager.setRepeating(AlarmManager.RTC_WAKEUP,mcalender.getTimeInMillis(),6000,mpendingIntent);
    
            }
        }
    }
    

    MyReceiver.java

    public class MyReceiver extends BroadcastReceiver {
    
        int MID = 0;
        int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        String date, time, school_name;
        PendingIntent pendingIntent;
        int intent_id;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // long when = System.currentTimeMillis();
            Bundle bundle = intent.getExtras();
            date = bundle.getString("date");
            time = bundle.getString("time");
            school_name = bundle.getString("school_name");
            intent_id = bundle.getInt("intent_id");
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Intent notificationIntent = new Intent(context, MainActivity.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            //final int intent_id = (int) System.currentTimeMillis();
            pendingIntent = PendingIntent.getActivity(context, intent_id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                    .setContentTitle("Notification From Dextro")
                    .setContentText("Meeting Schedule at " + school_name + " on " + date + " and time is " + time)
                    .setSmallIcon(R.mipmap.dextro_customerlogo)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setSound(alarmSound)
                    .setTicker("Notification From Dextro")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText("Meeting Schedule at " + school_name + " on " + date + " and time is " + time))
                    .setVibrate(new long[]{100, 100, 100, 100});
            notificationManager.notify(m, builder.build());
            MID++;
        }
    }
    

1。简单的方式当我重新安排我的工作时,它应该通知我     第二天早上有警报

1 个答案:

答案 0 :(得分:0)

试试这个

.setVibrate(new long[]{0, 2000, 500, 2000, 500, 2000, 500});
    try {
            Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bells_bells_bells); //your sound file name from Raw folder
            .setSound(uri);
            } catch (Exception e) {
                e.printStackTrace();
        }