2013-03-28 5 views
4

나는 알람 프로젝트를하고있는 중이 야에서 한 번 알람,어떻게</p> <p>내 코드가, 내가 한 번 알람을 설정할 ...하지만 난이 문신을 설정하는 문제에 직면하고 안드로이드

을 설정;

와트 실수가 난 내 코드에서 수행 한 다음 앱을 열 때 알람 푸시는 항상 활성화되어

public void loadCalender(String month) { 

    try { 

     Cursor cursor = null; 

     Database db = new Database(getApplicationContext()); 

     cursor = db.getSelectedCalenderDetails(month); 
     if (cursor.getCount() != 0) { 
      if (cursor.moveToFirst()) { 
       do { 
        String text = cursor.getString(cursor 
          .getColumnIndex("event")); 
        String title = "News/Events"; 
        String dates = cursor.getString(cursor 
          .getColumnIndex("date")); 

        String yr = dates.substring(0, 4); 
        int year = Integer.parseInt(yr); 
        String mon = dates.substring(5); 
        String mo = mon.substring(0, 2); 
        int months = Integer.parseInt(mo); 
        String da = dates.substring(9); 
        int day = Integer.parseInt(da); 

        // Ask our service to set an alarm for that date, 
        // this 
        // activity talks to the client that talks to the 
        // service 
        set_alarm(year, months, day, title, text); 
        System.out.println(dates); 

       } while (cursor.moveToNext()); 

      } 

     } 
     cursor.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    // looping through All Contacts 

} 


public void set_alarm(int year, int month, int day, String title, 
     String text) { 
    SharedPreferences prefs = PreferenceManager 
      .getDefaultSharedPreferences(getApplicationContext()); 

    // etc 
    Calendar cal = Calendar.getInstance(); 
    int hour = prefs.getInt("PREF_HOUR", 0); 
    int min = prefs.getInt("PREF_MIN", 0); 
    if(hour == 0 && min == 0){ 


    cal.set(Calendar.MONTH, month - 1); 
    cal.set(Calendar.YEAR, year); 
    cal.set(Calendar.DAY_OF_MONTH, 7); 

    cal.set(Calendar.HOUR_OF_DAY, 15); 
    cal.set(Calendar.MINUTE, min); 
    }else 
    { 

     cal.set(Calendar.MONTH, month - 1); 
     cal.set(Calendar.YEAR, year); 
     cal.set(Calendar.DAY_OF_MONTH, hour); 

     cal.set(Calendar.HOUR_OF_DAY, min); 
     cal.set(Calendar.MINUTE, min); 
    } 
    Intent intent = new Intent(getApplicationContext(), AlarmActivity.class); 
    intent.putExtra("title", title); 
    intent.putExtra("text", text); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
      getApplicationContext(), 1, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    AlarmManager alarmManager = (AlarmManager) getApplicationContext() 
      .getSystemService(getApplicationContext().ALARM_SERVICE); 

    /* 
    * alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
    * pendingIntent); 
    */ 

    alarmManager.cancel(pendingIntent); // cancel any existing alarms 

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY, 

      pendingIntent); 
} 

... .. PLZ 한 시간 알람

을 설정하는 데 도움이 대신 한 번에 알람을

답변

9

을 당신이 대신 setAlarm() 방법을 사용할 필요가 설정 alarmManager.setRepeating이 alarmManager.set를 사용하여 미리 감사드립니다

+0

또한 장치 부팅을 처리해야합니까? 또는 장치를 다시 시작한 후에도 계속 작동합니까? 기기가 꺼져 있어야했는데 (예 : 배터리 없음) 기기가 켜져 있었습니까? –

+0

@androiddeveloper 당신은 장치 부팅에 대해 걱정할 필요가 없습니다. RTC 방법은 특정 시간으로 알람을 설정합니다. 따라서 전화가 그 시간에 실행되지 않으면 다음 번에 전화가 부팅 될 때 경보가 발령됩니다. – Senhor

+0

@ 센고 알겠습니다. 시간 변경의 경우 (예 : '일광 절약 시간')은 어떻게됩니까? X에 알람을 설정하면 X가 나오기 전에 X는 X가 변경되기 전에 X를 X + 1로 변경합니다. 이것은 1 시간이 건너 뛴 것을 의미합니다. 그것은 경보에 무엇을 할 것인가? –

3

alarmManager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent); 
5

어떻게 setRepeating() 내가

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 

을 사용하고 있는데 나는에 통지 설정 allarm의 MENAGER를 호출하는 동안은, alarmMenager 응용 프로그램이 계속 실행되는 동안 그것은 작동 에 의해 미래에 알림을 밀어 원하는에 대해 내 알림이 표시되지 않은 futer 및 close app. 내가해야 할 일이 있습니까?

Calendar cal = Calendar.getInstance(); 
cal.set(Calendar.HOUR, HOUR_OF_DAY); 
cal.set(Calendar.MINUTE, MINUTE); 
//cal.add(Calendar.SECOND, SECOND_OF_DAY); 
Intent intent = new Intent(UnityPlayer.currentActivity, TimeAlarm.class); 
intent.putExtra("alarm_status", statusMessage); 
intent.putExtra("alarm_title", title); 
intent.putExtra("alarm_content", content); 
Log.i("SenderEvent ", "przygotowane dane"); 
PendingIntent sender = PendingIntent.getBroadcast(UnityPlayer.currentActivity.getApplicationContext(), REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 

그리고 리시버 : 응용 프로그램이 가까이 동안

Bundle bundle = intent.getExtras(); 
     String statusMessage = bundle.getString("alarm_status"); 
     String title = bundle.getString("alarm_title"); 
     String content = bundle.getString("alarm_content"); 
     nm = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
       new Intent(), 0); 
Notification notif =new Notification();//R.drawable.ic_launcher,statusMessage, System.currentTimeMillis());; 
     //notif.largeIcon = bitmap; 
     notif.icon =2130837504; 
     notif.tickerText=statusMessage; 
     notif.when= System.currentTimeMillis(); 
     /* 
     new Notification(0, 
       statusMessage, System.currentTimeMillis());*/ 
     notif.setLatestEventInfo(context, title, content, contentIntent); 
     nm.notify(NOTIFY_ME_ID, notif); 

가 무슨 잘못이와 미래에 알림을 밀어 여기

유 내 이벤트 보낸 사람있어?