홈>
안드로이드에서 매일 알림을 받기 위해 alarmanager를 시도했습니다 .... 알람은 지정된 시간에 시작되지만 그 후 1 분마다 반복됩니다 ... Mainactivity의 setrepeating () 함수에 INTERVAL_DAY를 지정했지만 그렇게합니다. 작동하지 않는 것 같습니다. Mainactivity, MyReceiver&AlarmService의 세 부분으로 구성됩니다. 누구든지 이것을 고칠 수 있습니까 ??
유지 관리
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
// we can set time by open date and time picker dialo
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 10);
calendar.set(Calendar.SECOND, 0);
Intent intent1 = new Intent(MainActivity.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(this.ALARM_SERVICE);
Log.e("Tag","calling here");
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),864000, pendingIntent);
}
}
내 수신자
public class MyReceiver extends BroadcastReceiver{
int MID=0;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.icon)
.setContentTitle("Alaram Fired")
.setContentText("Events To be PErformed").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
MyAlarmService
public class MyAlarmService extends Service
{
private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
Log.e("Tag1","alarmservice here");
super.onStart(intent, startId);
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
Notification notification = new Notification(R.drawable.icon,"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_CANCEL_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//notification.setLatestEventInfo(this.getApplicationContext(), "Daily Notification Demo", "This is a test message!", pendingNotificationIntent);
mManager.notify(0, notification);
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
- 답변 # 1
관련 자료
- java - Volley를 사용하여 JSON 구문 분석에서 Android Recyclerview 어댑터 문제
- android 스튜디오 41 - xml 리소스 id 자동 가져 오기 문제
- Android Google Map SDK Toolbar issue - Android Google Map SDK 툴바 문제 — 외부지도가 실행되지 않음
- kotlin - Android의 Room 데이터베이스 문제
- Android 11의 SoundPool 문제
- Android의 제약 레이아웃 문제
- Android Studio 패킷 구문 분석 문제 apk
- java - 맞춤 Android Spinner 레이아웃 문제
- reactjs - 네이티브 반응 - android 시뮬레이터의 텍스트 변환 문제
- react native - rn android 빌드 문제 - 태스크 ': app : packagedebug'에 대한 실행 실패
- javascript - Android 모바일 관련 문제 (100vh 및 위치 고정 바닥 글 버그)
- Android Studio 332 라이브러리 개정판 2800에 대한 내 문제
- angular - Android 기기의 이온 이미지 디스플레이 문제
- Android ADK 라이센스 문제로 인해 Jenkins 작업 실패
관련 질문
- java : 화면이 잠겨 있거나 꺼져 있을 때 카메라 셔터 onkeyevent가 있는 Android 앱
- java : Firebase 실시간 데이터베이스(android studio)를 읽거나 쓸 수 없습니다.
- java : SQLite는 열 값의 일부로 레code를 필터링합니다.
- Android Java가 실행되지 않음 [중복]
- java : Android 그래픽으로 그림판에서 그래프 그리기
- java : bumblebee android studio 버전으로 업데이트할 때 오류 gradle
- java : Icon MenuItem 안드로이드 앱을 변경하는 방법
- java : 앱에 암시적 내부 의도 취약점이 있습니다.
- java : Android 앱 -지도 조각에서 사라지는 Google 지도 위치 마커
- java : URI를 업데이트하려고 할 때 FileNotFoundException이 발생하는 ActivityResultContracts.TakePicture()
AlarmManager.setRepeating이 다른 Android 버전에서 제대로 작동하지 않습니다.
setExact를 시도하십시오. 반복되지는 않지만 아래 언급 된대로 반복 기능을 수행 할 수 있습니다.
MyReceiver 업데이트
여기서는 864000 + System.currentTimeMillis ()를 사용하여 nextAlarmTime을 계산하여 알람을 다시 예약합니다.