繁体   English   中英

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

[英]I want set a notification that it should ring when i receive a notification

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

     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.以简单的方式当我重新安排工作时,它应该在第二天早上通知我,并发出警报

尝试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM