我想在特定日期为特定工作日设置闹钟?

时间:2014-03-03 06:58:51

标签: android android-alarms

我想在特定时间的特定日期实施警报,如何通过用户选择工作日来实施,是他们重复日的任何教程,我正在尝试这个,请帮助我如何实现用户选择的日期?< / p>

public class MainActivity extends Activity {

    Calendar calNow, calSet;

    // call this method for alram setAlarm(calSet);
    TimePicker myTimePicker;
    Button buttonstartSetDialog;
    Button buttonCancelAlarm;
    TextView textAlarmPrompt;
    ListView listView_weekly;
    TimePickerDialog timePickerDialog;
    int mSelectedPosition_week, mSelectedPosition_month = 1;
    final static int RQS_1 = 1;
    protected static final String TAG = null;
    private ReminderFreqSelectionAdapter mAdapter_week, mAdapter_month = null;

    List<String> weekly_list, monthly_list, empty_list;

    Integer[] Selectedweeklist;

    String[] weekly_arry = new String[] { "Sunday", "Monday", "Tuesday",
            "Wednesday", "Thursday", "Friday", "Saturday" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView_weekly = (ListView) findViewById(R.id.list_weekly);

        listView_weekly.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        textAlarmPrompt = (TextView) findViewById(R.id.alarmprompt);

        buttonstartSetDialog = (Button) findViewById(R.id.startSetDialog);
        buttonstartSetDialog.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                textAlarmPrompt.setText("");
                openTimePickerDialog(false);

            }
        });
        weekly_list = new ArrayList<String>();
        Selectedweeklist = new Integer[7];

        for (int i = 0; i < weekly_arry.length; i++) {
            String item = weekly_arry[i];
            weekly_list.add(item);
        }

        System.out.println("weekly_list size: " + weekly_list.size());

        mAdapter_week = new ReminderFreqSelectionAdapter(this, weekly_list);
        listView_weekly.setAdapter(mAdapter_week);



    }

    private void openTimePickerDialog(boolean is24r) {
        Calendar calendar = Calendar.getInstance();

        timePickerDialog = new TimePickerDialog(MainActivity.this,
                onTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), is24r);
        timePickerDialog.setTitle("Set Alarm Time");

        timePickerDialog.show();

    }

    OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            calNow = Calendar.getInstance();
            calSet = (Calendar) calNow.clone();

            calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calSet.set(Calendar.MINUTE, minute);
            calSet.set(Calendar.SECOND, 0);
            calSet.set(Calendar.MILLISECOND, 0);

            if (calSet.compareTo(calNow) <= 0) {
                // Today Set time passed, count to tomorrow
                calSet.add(Calendar.DATE, 1);
            }

            // setAlarm(calSet);

            daysetAlarm(2);
        }
    };

    public void daysetAlarm(int dayOfWeek) {
        // Add this day of the week line to your existing code
        System.out.println("alram calling");
        Calendar alarmCalendar = Calendar.getInstance();

        alarmCalendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);

        Calendar time = calSet;
        Long timeeset = time.getTimeInMillis();

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        // Also change the time to 24 hours.
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeeset,
                24 * 60 * 60 * 1000, pendingIntent);
    }

    private void setAlarm(Calendar targetCal) {

        textAlarmPrompt.setText("\n\n***\n" + "Alarm is set@ "
                + targetCal.getTime() + "\n" + "***\n");

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                targetCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
                pendingIntent);


    }

    private void cancelAlarm() {

        textAlarmPrompt.setText("\n\n***\n" + "Alarm Cancelled! \n" + "***\n");

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);

    }

    class ReminderFreqSelectionAdapter extends BaseAdapter {
        Context context;
        LayoutInflater inflater;
          private Activity activity;

        public ReminderFreqSelectionAdapter(Context ctx,
                List<String> weekly_list) {
            this.context = ctx;
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return weekly_list.size();
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView,
            ViewGroup parent) {

        View v = convertView;
         ViewHolder holder;   
        if (v == null) {
            holder = new ViewHolder();
            v = inflater.inflate(R.layout.elist, null);
            holder.title_tv = (TextView) v.findViewById(R.id.text);

            holder.chekdTv = (CheckBox) v
                    .findViewById(R.id.checkbox);

            v.setTag(holder);

        } else {
            holder = (ViewHolder) v.getTag();

        }

        holder.title_tv.setText(weekly_list.get(position));


        holder.chekdTv.setOnClickListener(new OnClickListener() {
             private int mPosition;


              void OnItemClickListener(int position){
                  mPosition = position;
             }
              @Override
              public void onClick(View v) {
                        //is chkIos checked?
                if (((CheckBox) v).isChecked()) {
                                 //Case 1
                     MainActivity sct = (MainActivity)context;

                      /****  Call  onItemClick Method inside CustomListViewAndroidExample Class ( See Below )****/

//                       sct.onItemClick(mPosition);

                    System.out.println("check box checking");
                }
              }
            });
          v.setOnClickListener(new OnItemClickListener( position ));

        return v;
    }

          private class OnItemClickListener  implements OnClickListener{           
                 private int mPosition;

                 OnItemClickListener(int position){
                      mPosition = position;
                 }

                 @Override
                 public void onClick(View arg0) {


                   MainActivity sct = (MainActivity)context;


                  //****  Call  onItemClick Method inside CustomListViewAndroidExample Class ( See Below )****//*

                     sct.onItemClick(mPosition);
                 }               
             }   


        class ViewHolder {
            TextView title_tv;
            CheckBox chekdTv;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            // System.out.println("itme: "+listView.getItemAtPosition(position));
            return null;
        }

    }

    public void onItemClick(int mPosition) {
        // TODO Auto-generated method stub

        System.out.println("position: "+mPosition);
        Toast.makeText(getApplicationContext(), ".dkfd", 1).show();
    }

}

my main xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/startSetDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Set Target Time" />


    <TextView
        android:id="@+id/alarmprompt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/list_weekly"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="25dp" />

</LinearLayout>
my weekdays list view xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/attributesCLL"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.0"
        android:paddingLeft="15dip"
        android:textSize="16dip" />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="4dip"
        android:checkMark="?android:attr/textCheckMark"
        android:ellipsize="marquee"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

0 个答案:

没有答案