Computer >> 컴퓨터 >  >> 프로그램 작성 >> Android

Android 알림을 미래의 특정 날짜로 설정하는 방법은 무엇입니까?

<시간/>

이 예는 Android 알림을 미래의 특정 날짜로 설정하는 방법을 보여줍니다.

1단계 − Android Studio에서 새 프로젝트를 생성하고 파일 ⇒ 새 프로젝트로 이동하여 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.

2단계 − res/layout/activity_main.xml에 다음 코드를 추가합니다.

<전>

3단계 − src/MainActivity에 다음 코드를 추가합니다.

패키지 app.tutorialspoint.com.notifyme;import android.app.AlarmManager;import android.app.DatePickerDialog;import android.app.Notification;import android.app.PendingIntent;import android.content.Context;import android. content.Intent;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android. widget.DatePicker;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Locale;public class MainActivity extends AppCompatActivity { public static final String NOTIFICATION_CHANNEL_ID ="10001"; 개인 최종 정적 문자열 default_notification_channel_id ="기본"; 버튼 btn날짜; 최종 캘린더 myCalendar =캘린더. getInstance(); @Override 보호된 무효 onCreate(SavedInstanceState 번들) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnDate =findViewById(R.id. btnDate ); } private void scheduleNotification(알림 알림, 긴 지연) { Intent notificationIntent =new Intent( this, MyNotificationPublisher.class ); notificationIntent.putExtra(MyNotificationPublisher. NOTIFICATION_ID, 1); notificationIntent.putExtra(MyNotificationPublisher. NOTIFICATION, 알림); PendingIntent PendingIntent =PendingIntent. getBroadcast( this, 0, notificationIntent, PendingIntent. FLAG_UPDATE_CURRENT); AlarmManager alarmManager =(AlarmManager) getSystemService(Context.ALARM_SERVICE); 알람 관리자를 주장하십시오! =null; alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 지연, pendingIntent); } 개인 알림 getNotification(문자열 내용) { NotificationCompat.Builder 빌더 =new NotificationCompat.Builder( this, default_notification_channel_id ); builder.setContentTitle( "예약 알림" ); builder.setContentText(콘텐츠); builder.setSmallIcon(R.drawable.ic_launcher_foreground); builder.setAutoCancel( true ); 빌더.setChannelId( NOTIFICATION_CHANNEL_ID ); 반환 builder.build(); } DatePickerDialog.OnDateSetListener 날짜 =new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet (DatePicker 보기, int year, int monthOfYear, int dayOfMonth) { myCalendar .set(Calendar.YEAR, year); myCalendar .set(Calendar.MONTH,monthOfYear); myCalendar .set(달력.DAY_OF_MONTH, dayOfMonth); 업데이트 라벨(); } }; public void setDate (보기 보기) { new DatePickerDialog( MainActivity. this, date , myCalendar .get(Calendar. YEAR ) , myCalendar .get(Calendar.MONTH ) , myCalendar .get(Calendar.DAY_OF_MONTH ) ).show(); } 개인 무효 updateLabel() { 문자열 myFormat ="dd/MM/yy"; // 여기에 넣어야 할 곳 SimpleDateFormat sdf =new SimpleDateFormat(myFormat , Locale.getDefault ()); 날짜 날짜 =myCalendar .getTime(); btnDate .setText(sdf.format(날짜)); 일정 알림(getNotification( btnDate .getText().toString()) , 날짜.getTime()); }}

4단계 − src/MyNotificationPublisher에 다음 코드를 추가합니다.

패키지 app.tutorialspoint.com.notifyme;import android.app.Notification;import android.app.NotificationChannel;import android.app.NotificationManager;import android.content.BroadcastReceiver;import android.content.Context;import android. content.Intent; 정적 app.tutorialspoint.com.notifyme.MainActivity 가져오기. NOTIFICATION_CHANNEL_ID;공개 클래스 MyNotificationPublisher는 BroadcastReceiver를 확장합니다. { public static String NOTIFICATION_ID ="notification-id"; 공개 정적 문자열 NOTIFICATION ="알림"; 공개 무효 onReceive (컨텍스트 컨텍스트, 의도 의도) { NotificationManager notificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 알림 알림 =intent.getParcelableExtra( NOTIFICATION ); if (android.os.Build.VERSION. SDK_INT>=android.os.Build.VERSION_CODES. O ) { int 중요도 =NotificationManager. IMPORTANCE_HIGH; NotificationChannel notificationChannel =new NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , 중요도); 어설션 notificationManager !=null; NotificationManager.createNotificationChannel(notificationChannel); } int id =의도.getIntExtra( NOTIFICATION_ID , 0 ); 어설션 notificationManager !=null; notificationManager.notify(id, 알림); }}

5단계 − AndroidManifest.xml에 다음 코드 추가

<전> <카테고리 안드로이드 :name ="android.intent.category.LAUNCHER" /> <수신자 안드로이드 :name=".MyNotificationPublisher" />

응용 프로그램을 실행해 보겠습니다. 실제 Android 모바일 장치를 컴퓨터에 연결했다고 가정합니다. Android 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 도구 모음에서 실행 아이콘을 클릭합니다. 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오 -