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

Android에서 미리 알림 알림을 만드는 방법은 무엇입니까?

<시간/>

이 예는 Android에서 미리 알림 알림을 만드는 방법을 보여줍니다.

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

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

<전> <버튼 android:onClick="createNotification" android:text="만들기 알림" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="wrap_content" />

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

패키지 app.tutorialspoint.com.notifyme;import android.app.AlarmManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import java.util.Calendar;public class MainActivity extends AppCompatActivity { @Override protected void onCreate (Bundle storedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } 공개 무효 createNotification(보기 보기) { 의도 myIntent =new Intent(getApplicationContext(), NotifyService.class); 알람매니저 알람매니저 =(알람매니저) getSystemService( ALARM_SERVICE ); PendingIntent PendingIntent =PendingIntent. getService( this, 0, myIntent, 0); 캘린더 캘린더 =캘린더. getInstance(); Calendar.set(Calendar. SECOND , 0 ); Calendar.set(달력.분, 0); Calendar.set(달력.시간, 0); Calendar.set(Calendar.AM_PM , Calendar.AM ); Calendar.add(달력.DAY_OF_MONTH, 1); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent); }}

4단계 − src/NotifyService에 다음 코드 추가

패키지 app.tutorialspoint.com.notifyme;import android.app.NotificationChannel;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android. os.IBinder;import android.support.v4.app.NotificationCompat;public class NotifyService extends Service { public static final String NOTIFICATION_CHANNEL_ID ="10001"; 개인 최종 정적 문자열 default_notification_channel_id ="기본"; public NotifyService() { } @Override public IBinder onBind(의도 의도) { 의도 notificationIntent =new Intent(getApplicationContext(), MainActivity.class); notificationIntent.putExtra( "fromNotification" , true ); notificationIntent.setFlags(의도.FLAG_ACTIVITY_CLEAR_TOP | 의도.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent PendingIntent =PendingIntent. getActivity( this, 0, notificationIntent, 0); 알림 관리자 mNotificationManager =(알림 관리자) getSystemService( NOTIFICATION_SERVICE ); NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(getApplicationContext(), default_notification_channel_id); mBuilder.setContentTitle( "내 알림" ); mBuilder.setContentIntent(pendingIntent); mBuilder.setContentText( "알림 리스너 서비스 예제" ); mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground); mBuilder.setAutoCancel( true ); 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" , 중요도); mBuilder.setChannelId( NOTIFICATION_CHANNEL_ID ); 어설션 mNotificationManager !=null; mNotificationManager.createNotificationChannel(notificationChannel); } 어설션 mNotificationManager !=null; mNotificationManager.notify(( int ) 시스템.currentTimeMillis() , mBuilder.build()); throw new UnsupportedOperationException( "아직 구현되지 않음" ); }}

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

<전> <카테고리 안드로이드 :이름 ="android.intent.category.LAUNCHER" />

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

Android에서 미리 알림 알림을 만드는 방법은 무엇입니까?