2016-08-20 2 views
1

Alarm Manager를 사용하여 브로드 캐스트를 송수신하고 알림을 생성하는 메소드를 호출하려고합니다. 앱이 종료 된 경우에도 알림이 생성되기를 바랍니다. 나는 경보 관리자에서 경보를 생성이 방법으로 클래스를 생성 :MainActivity의 Android 클래스가 Alarm Manager에서 브로드 캐스트를 수신하지 않습니다.

... 
<application> 
    <receiver android:name=".MainActivity$loadqotd"></receiver> 
</application> 
... 
:이 매니페스트입니다

public class loadqotd extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // testing purposes 
     Toast.makeText(context, "received", Toast.LENGTH_SHORT).show(); 
     Intent resultIntent = new Intent(context, QuestionOfTheDay.class); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 

     mBuilder.setSmallIcon(R.drawable.notification_icon) 
         .setContentTitle("Your Question of the Day is ready!") 
         .setContentText("Check out the question of today") 
         .setContentIntent(PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT)); 

     int notid = 001; 
     NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     notificationManager.notify(notid, mBuilder.build()); 
    } 
} 

:

@android.webkit.JavascriptInterface 
public void qotd(String toast, int hour, int minute) { 
      // for testing purposes 
      Toast.makeText(mContext, (String.valueOf(hour) + " " + String.valueOf(minute)), Toast.LENGTH_SHORT).show(); 

      // cancel other alarms first 
      Intent alarmtocancel = new Intent(mContext, loadqotd.class); 
      PendingIntent cancelIntent = PendingIntent.getBroadcast(mContext, 1001, alarmtocancel, PendingIntent.FLAG_CANCEL_CURRENT); 
      AlarmManager am = (AlarmManager) getSystemService(mContext.ALARM_SERVICE); 
      am.cancel(cancelIntent); 
      cancelIntent.cancel(); 

      // create new alarm 
      Intent alarmintent = new Intent(mContext, loadqotd.class); 
      AlarmManager setam = (AlarmManager) getSystemService(mContext.ALARM_SERVICE); 
      PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, alarmintent, 0); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.set(Calendar.HOUR_OF_DAY, hour); 
      calendar.set(Calendar.MINUTE, minute); 
      setam.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);    
} 

이 수신기가 나는 주요 활동의 내부에 그것을 배치

나는 Alarm Manager 덤프를 수행했으며 내부에서 브로드 캐스트를 발견했다. 그러나 알람 관리자가 실행되면 앱이 백그라운드에 있거나 앱에있을 때도 loadqotd는 호출되지 않습니다. 또한 빌드 오류나 런타임 예외 때문도 아닙니다.

+0

응용 프로그램 구성 요소에 사용되는 중첩 클래스 (예 :'loadqotd' 클래스)는'public static'이어야합니다. 또한 Java 명명 규칙에 따라 제안하고 클래스 이름을 대문자로 사용하십시오. –

+0

0이 아닌 요청 코드 사용 시도 : PendingIntent pendingIntent = PendingIntent.getService (mContext, NON_ZERO_INTEGER, alarmintent, 0); – Amir

+0

@MikeM. 그것은 오류가 아닌 정적 메서드 getSystemService (문자열) 정적 문맥에서 참조 할 수 없다는 것 – zehata

답변

1

질문에 loadqotd 클래스처럼 중첩 된 클래스를 앱 구성 요소로 사용하는 경우 public static이어야합니다. 또는 별도의 클래스 파일로 옮겨서 매니페스트의 name을 적절하게 업데이트 할 수 있습니다.

어떤 방법을 선택하든, Receiver 클래스에서 몇 가지를 변경해야합니다. 이전에 현재 MainActivity 인스턴스에서 호출되었으므로 onReceive() 메서드에 전달 된 Context에서 getSystemService()으로 전화해야합니다.이 인스턴스는 더 이상 액세스 할 수 없습니다. 또한 NOTIFICATION_SERVICE 상수를 한정해야합니다.

NotificationManager notificationManager = 
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

는 그리고, 마지막으로, 알람에 대한 PendingIntent 당신이 Service을 시작하지, 수신기에 방송하고 있기 때문에, 방송 PendingIntent 할 필요가있다. getService()getBroadcast()으로 변경하십시오.

PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, alarmintent, 0); 
관련 문제