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는 호출되지 않습니다. 또한 빌드 오류나 런타임 예외 때문도 아닙니다.
응용 프로그램 구성 요소에 사용되는 중첩 클래스 (예 :'loadqotd' 클래스)는'public static'이어야합니다. 또한 Java 명명 규칙에 따라 제안하고 클래스 이름을 대문자로 사용하십시오. –
0이 아닌 요청 코드 사용 시도 : PendingIntent pendingIntent = PendingIntent.getService (mContext, NON_ZERO_INTEGER, alarmintent, 0); – Amir
@MikeM. 그것은 오류가 아닌 정적 메서드 getSystemService (문자열) 정적 문맥에서 참조 할 수 없다는 것 – zehata