单击listView中的特定位置时,如何取消警报

时间:2017-07-11 08:49:09

标签: java android listview alarmmanager android-pendingintent

我的应用程序让用户在listView上创建4个警报并显示4个警报,当用户点击listView中的项目时可以删除警报,但这并不比我想象的简单。我需要知道的是当我点击listView ????上的项目(闹钟)时如何取消pendingIntentArr中的pendingIntent

以下是我的代码:

public class MainActivity extends Activity {

    AlarmManager alarmManager;
    TimePicker timePicker;
    Button button1, button2;
    Calendar calendar;
    PendingIntent[] pendingIntentArr;

    ArrayList<String> alarms;
    //ArrayList<PendingIntent> intentArray;
    Intent intent;
    ListView listView;
    ArrayAdapter<String> arrayAdapter;
    PendingIntent pendingIntent;



    String gio, phut, hengio;

    int i = 0,j=0;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     /* if (savedInstanceState != null) {
            String[] values = savedInstanceState.getStringArray("myKey");
            arrayAdapter.add(values);
            listView.setAdapter(arrayAdapter);

       }*/



        pendingIntentArr = new PendingIntent[5];

        //Khai báo;
        alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        timePicker = (TimePicker) findViewById(R.id.timePicker2);
        calendar = Calendar.getInstance();
        button1 = (Button) findViewById(R.id.button7);
        button2 = (Button) findViewById(R.id.button8);
        listView = (ListView) findViewById(R.id.listView);

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

        alarms = new ArrayList<String>();

        intent = new Intent(MainActivity.this, AlarmService.class);


        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                    calendar.set(Calendar.HOUR_OF_DAY, timePicker.getHour());
                    calendar.set(Calendar.MINUTE, timePicker.getMinute());


                    gio = String.valueOf(timePicker.getHour());
                    phut = String.valueOf(timePicker.getMinute());

                    hengio = "Nhắc nhở" + gio + ":" + phut;


                    alarms.add(i,hengio);//dùng để gán listView

                    arrayAdapter = new ArrayAdapter(MainActivity.this, R.layout.layout, R.id.textView, alarms);


                    listView.setAdapter(arrayAdapter);

            //==================================================================================================


                    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, j, intent,0);


                   // alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);


                    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);


                    //intentArray.add(i,pendingIntent);//#

                    pendingIntentArr[i]=pendingIntent;



                    if(alarms.size()<4)
                    {

                    button1.setEnabled(true);
                    i++;

                    }

                    else {

                        i=0;
                        button1.setEnabled(false);
                        Toast.makeText(MainActivity.this,"Alarm is full",Toast.LENGTH_SHORT).show();
                    }
            j++;


        }

    });


    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            for(int delete = 0; delete=pendingIntentArr.length;delete++){

                alarmManager.cancel(pendingIntentArr[delete]);

            }





        }
    });


  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {




          Toast.makeText(MainActivity.this,alarms.get(position)+"Đã được xóa",Toast.LENGTH_SHORT).show();



          //alarmManager[position].cancel(intentArray.get(position));//cancel các pendingIntent tại các position được click.

          //intentArray.get(position).cancel();

          alarmManager.cancel(pendingIntentArr[position]);

          //pendingIntentArr[position]=null;

          alarms.remove(position);//xóa các object alarms

         // alarmManager[position]=null;

            i=position;





          if (alarms.size()<4) button1.setEnabled(true);//Kiem tra nếu số lượng alarms nhỏ hơn 4 thì sẽ cho phép thêm alarm còn không thì sẽ disable button1.
          else button1.setEnabled(false);


          arrayAdapter.notifyDataSetChanged();




      }
  });



}








}

0 个答案:

没有答案