java-如何避免动态使用相同消息的相同时间间隔覆盖警报管理器?

如果我们为两条不同的消息选择相同的时间间隔而不发送各自的消息,它将接收第一条消息内容并发送第二条短信时间,将会发生什么.
我想使用相同的间隔发送不同的短信.

MyAlaramactivity.java

否则,如果(mytime.equals(“每隔5分钟”)){

                        editor1.putString("p1",edittextSmsNumber.getText().toString());
                        editor1.putString("m1",edittextSmsText.getText().toString());
                        editor1.commit();
                        edittextSmsNumber.setText(" ");
                        edittextSmsText.setText(" ");


                        Calendar calendar = Calendar.getInstance();

                        calendar.set(datep.getYear(), datep.getMonth(),
                                datep.getDayOfMonth(),
                                timep.getCurrentHour(),
                                timep.getCurrentMinute(), 0);
                        Intent myIntent1 = new Intent(AndroidAlarmSMS.this,
                                MyAlarmService.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("service", 1);
                        myIntent1.putExtras(bundle);
                        //myIntent1.putExtra("service", "s1");
                        myIntent1.setData(Uri.parse("timer:myIntent1"));
                        pendingIntent1 = PendingIntent.getService(
                                AndroidAlarmSMS.this, 1, myIntent1, PendingIntent.FLAG_UPDATE_CURRENT);

                        AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
                        alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,
                                calendar.getTimeInMillis(), 1000 * 60 * 2,
                                pendingIntent1);




                        // Millisec * Second *
                                                // Minute
                    } else if (mytime.equals("Every hour")) {
                        editor2.putString("p2",edittextSmsNumber.getText().toString());
                        editor2.putString("m2",edittextSmsText.getText().toString());
                        editor2.commit();
                        edittextSmsNumber.setText(" ");
                        edittextSmsText.setText(" ");
                        Calendar calendar = Calendar.getInstance();

                        calendar.set(datep.getYear(), datep.getMonth(),
                                datep.getDayOfMonth(),
                                timep.getCurrentHour(),
                                timep.getCurrentMinute(), 0);
                        Intent myIntent2 = new Intent(AndroidAlarmSMS.this,
                                MyAlarmService.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("service", 2);
                        myIntent2.putExtras(bundle);
                        myIntent2.setData(Uri.parse("timer:myIntent2"));
                        pendingIntent2 = PendingIntent.getService(
                                AndroidAlarmSMS.this, 2, myIntent2, PendingIntent.FLAG_UPDATE_CURRENT);

                        AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);

                        alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,
                                calendar.getTimeInMillis(), 1000 * 60 * 3,
                                pendingIntent2); 




This is my service class



public void onStart(Intent intent, int startId) {
  super.onStart(intent, startId);
  Bundle bundle = intent.getExtras();
  //String status= b.getString("service");
  status = bundle.getInt("service", 0);


  if(i==2)
   {
   i++;
   Toast.makeText(this, "Hiii", Toast.LENGTH_LONG).show();
   }
   else
   {
       String share_pref_file = "IMS";
          SharedPreferences prefs = getSharedPreferences(share_pref_file,
          Context.MODE_PRIVATE);
       if(status == 1)
       {
           String number = prefs.getString("p1", "");
           String message = prefs.getString("m1", "");
              //String message= prefs.getString("extraSmsText", "");

              Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
              Toast.makeText(this,
                     number,
                     Toast.LENGTH_LONG).show();

              SmsManager smsManager = SmsManager.getDefault();
              smsManager.sendTextMessage(number, null, message, null, null);  
       }
       else if (status == 2)
       {
           String share_pref_file1 = "IMS2";
              SharedPreferences prefs2 = getSharedPreferences(share_pref_file1,
              Context.MODE_PRIVATE);
           String number = prefs2.getString("p2", "");
           String message = prefs2.getString("m2", "");
              //String message= prefs.getString("extraSmsText", "");

              Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
              Toast.makeText(this,
                     number,
                     Toast.LENGTH_LONG).show();
              try{
                  SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(number, null, message, null, null);  
                      Toast.makeText(this, "Message sent successfully", Toast.LENGTH_LONG).show();
              }
              catch(Exception e)
              {
                  Toast.makeText(this,
                            "I will kill you enter correct number",
                             Toast.LENGTH_LONG).show(); 
              }


       }

必须避免在相同间隔内覆盖阿拉玛纳格

解决方法:

我查看了您的代码,因为要首先设置多个alaram,所以您必须创建alaram管理器数组并将其代码更改为这样..首先,您创建一个名称为Fivemin的全局整数变量

else if (mytime.equals("Every 5 Minutes")) {
fivemin=fivemin+1;

AlarmManager[] alarmManager=new AlarmManager[24];


ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();



Intent myIntent1 = new Intent(AndroidAlarmSMS.this,
                                    MyAlarmService.class);
                            Bundle bundle = new Bundle();
                    bundle.putInt("service", 1);
                    myIntent1.putExtras(bundle);


                            myIntent1.setData(Uri.parse("timer:myIntent1"));
                            pendingIntent = PendingIntent.getService(
                                    AndroidAlarmSMS.this, fivemin, myIntent1,0);

                            alarmManager[fivemin] =  (AlarmManager) getSystemService(ALARM_SERVICE);        
                            alarmManager[fivemin].setRepeating(AlarmManager.RTC_WAKEUP,
                                    calendar.getTimeInMillis(), 1000 * 60 * 2,
                                    pendingIntent);

                            intentArray.add(pendingIntent);
上一篇:tomcat部署成https协议


下一篇:QueryRunner(common-dbutils.jar)