|
|
@@ -12,7 +12,7 @@ import android.util.Log;
|
|
|
|
|
|
import com.xzjmyk.pm.activity.R;
|
|
|
import com.xzjmyk.pm.activity.ui.erp.util.CommonUtil;
|
|
|
-import com.xzjmyk.pm.activity.util.CalendarUtils;
|
|
|
+import com.xzjmyk.pm.activity.util.TimeUtils;
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
|
@@ -21,16 +21,19 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|
|
@Override
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
if (intent.getAction().equals(AlarmaActivity.KEY_ALAR_ACTION)) {//上班签到提醒
|
|
|
- String content = context.getResources().getString(R.string.alar_content);
|
|
|
- sendNotification(context, content, true);
|
|
|
+ sendNotification(context, false);//当前为上班 则下一次为下班
|
|
|
} else if (intent.getAction().equals(AlarmaActivity.KEY_UALAR_ACTION)) {//下班签到提醒
|
|
|
- String content = context.getResources().getString(R.string.alar_content);
|
|
|
- sendNotification(context, content, false);
|
|
|
+ sendNotification(context, true);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void sendNotification(Context context, String stringExtra, boolean isAlar) {
|
|
|
- Log.i("gongpengming", stringExtra);
|
|
|
+ /**
|
|
|
+ * 签到提醒弹出通知栏
|
|
|
+ *
|
|
|
+ * @param context
|
|
|
+ * @param isAlar 下一次提醒为上班还是下班(前一次为上班 下一次为下班)
|
|
|
+ */
|
|
|
+ private void sendNotification(Context context, boolean isAlar) {
|
|
|
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(
|
|
|
context.getApplicationContext());
|
|
|
@@ -38,58 +41,81 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|
|
builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
|
|
|
builder.setAutoCancel(true);
|
|
|
builder.setContentTitle("签到提醒");
|
|
|
+ CharSequence stringExtra = "";
|
|
|
+ if (isAlar) {
|
|
|
+ stringExtra = context.getResources().getString(R.string.alar_content);
|
|
|
+ } else {
|
|
|
+ stringExtra = context.getResources().getString(R.string.ualar_content);
|
|
|
+ }
|
|
|
builder.setContentText(stringExtra);
|
|
|
Intent intent = new Intent(context, AlarmaActivity.class);
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
builder.setContentIntent(pendingIntent);
|
|
|
mNotificationManager.notify(1000, builder.build());
|
|
|
- int year = CalendarUtils.getCurrentYear();
|
|
|
- int month = CalendarUtils.getCurrentMonth();
|
|
|
- int day = CalendarUtils.getCurrentDate();
|
|
|
- int hh = 0;
|
|
|
- int mm = 0;
|
|
|
- if (isAlar) {//当前为上班时间提醒
|
|
|
- //下班提醒
|
|
|
- hh = CommonUtil.getSharedPreferencesInt(context, AlarmaActivity.KEY_UALAR_HH);
|
|
|
- mm = CommonUtil.getSharedPreferencesInt(context, AlarmaActivity.KEY_UALAR_MM);
|
|
|
- } else {
|
|
|
- if (day == CalendarUtils.getCurrentMaxNumOfMonth()) {//为当月最大
|
|
|
- day = 1;
|
|
|
- if (month == 12) {
|
|
|
- month = 1;
|
|
|
- year++;
|
|
|
- } else {
|
|
|
- month++;
|
|
|
- }
|
|
|
- } else {
|
|
|
- day++;
|
|
|
- }
|
|
|
- hh = CommonUtil.getSharedPreferencesInt(context, AlarmaActivity.KEY_UALAR_HH);
|
|
|
- mm = CommonUtil.getSharedPreferencesInt(context, AlarmaActivity.KEY_UALAR_MM);
|
|
|
- }
|
|
|
- setArmmanager(context, year, month, day, hh, mm, isAlar);
|
|
|
+
|
|
|
+ //设置签到提醒
|
|
|
+ initArmmanager(context, isAlar);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- private void setArmmanager(Context context, int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minute, boolean isAlar) {
|
|
|
+ private void setArmmanager(Context context, Calendar calendar, boolean isAlar) {
|
|
|
//操作:发送一个广播,广播接收后Toast提示定时操作完成
|
|
|
Intent intent = new Intent(context, AlarmReceiver.class);
|
|
|
String actionArg;
|
|
|
if (isAlar)
|
|
|
- actionArg = AlarmaActivity.KEY_UALAR_ACTION;
|
|
|
- else
|
|
|
actionArg = AlarmaActivity.KEY_ALAR_ACTION;
|
|
|
-
|
|
|
+ else
|
|
|
+ actionArg = AlarmaActivity.KEY_UALAR_ACTION;
|
|
|
intent.setAction(actionArg);
|
|
|
- CommonUtil.setSharedPreferences(context, "ALARMA_ACTION", actionArg);
|
|
|
- AlarmManager alarm = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
|
|
|
+ AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
Calendar c = Calendar.getInstance();
|
|
|
- c.set(Calendar.YEAR, year);
|
|
|
- c.set(Calendar.MONTH, monthOfYear - 1);
|
|
|
- c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
|
|
|
- c.set(Calendar.HOUR_OF_DAY, hourOfDay);
|
|
|
- c.set(Calendar.MINUTE, minute);
|
|
|
- c.set(Calendar.SECOND, 0);
|
|
|
- c.set(Calendar.MILLISECOND, 0);
|
|
|
- alarm.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), PendingIntent.getBroadcast(context, 0, intent, 0));
|
|
|
+ c.setTimeInMillis(System.currentTimeMillis());
|
|
|
+ alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), getPendingIntent(context, intent));
|
|
|
+ }
|
|
|
+
|
|
|
+ private PendingIntent getPendingIntent(Context context, Intent intent) {
|
|
|
+ return PendingIntent.getBroadcast(context, 0, intent, 0);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置下一次提醒
|
|
|
+ *
|
|
|
+ * @param context context
|
|
|
+ * @param isAlar 需要提醒为上班还是下班,如果前一次为上班提醒,则为下班,如果前一次为下班,则为下班
|
|
|
+ */
|
|
|
+ private void initArmmanager(Context context, boolean isAlar) {
|
|
|
+ //先判断两个(上下班)的提醒那个距离当前时间最近
|
|
|
+ //将上下班时间设置为今天时间
|
|
|
+ String dateTime = "";
|
|
|
+ if (isAlar) {//要设置上班提醒
|
|
|
+ dateTime = CommonUtil.getSharedPreferences(context, AlarmaActivity.KEY_ALAR_TIME);//获取上班时间
|
|
|
+ } else {
|
|
|
+ dateTime = CommonUtil.getSharedPreferences(context, AlarmaActivity.KEY_UALAR_TIME);//获取下班时间
|
|
|
+ }
|
|
|
+ int[] hm = getHourAndM(dateTime);
|
|
|
+ String time = getTimeFormat(hm[0], hm[1]);
|
|
|
+ long timeLong = TimeUtils.f_str_2_long(time);
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTimeInMillis(timeLong);
|
|
|
+ setArmmanager(context, c, isAlar);
|
|
|
+ }
|
|
|
+
|
|
|
+ private int[] getHourAndM(String temp) {
|
|
|
+ int[] time = new int[2];
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTimeInMillis(TimeUtils.f_str_2_long(temp));
|
|
|
+ time[0] = c.get(Calendar.HOUR_OF_DAY);
|
|
|
+ time[1] = c.get(Calendar.MINUTE);
|
|
|
+ Log.i("gongpengming", "time[0]=" + time[0] + "@@ time[1]=" + time[1]);
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTimeFormat(int hh, int mm) {
|
|
|
+ String h = (hh < 10 ? "0" : "") + hh;
|
|
|
+ String m = (mm < 10 ? "0" : "") + mm;
|
|
|
+ String temp = TimeUtils.s_long_2_str(System.currentTimeMillis()) + " " + h + ":" + m + ":" + "00";
|
|
|
+ return temp;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|