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

Android에서 Notification.deleteIntent를 사용하는 방법은 무엇입니까?

<시간/>

이 예제는 Android에서 Notification.deleteIntent를 사용하는 방법을 보여줍니다.

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.NotificationChannel;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android. support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.view.View;public class MainActivity extends AppCompatActivity { public static final String NOTIFICATION_CHANNEL_ID ="10001"; 개인 최종 정적 문자열 default_notification_channel_id ="기본"; @Override protected void onCreate(SavedInstanceState 번들) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } 공개 무효 createNotification(보기 보기) { 의도 알림 인텐트 =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 ); mBuilder.setDeleteIntent(getDeleteIntent()); 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()); } 보호된 PendingIntent getDeleteIntent() { 의도 의도 =new Intent(MainActivity. this, NotificationBroadcastReceiver.class); 인텐트.setAction( "알림_취소" ); PendingIntent를 반환합니다. getBroadcast(MainActivity. this, 0, 인텐트, PendingIntent. FLAG_CANCEL_CURRENT); }}

4단계 - src/NotificationBroadcastReceiver

에 다음 코드 추가
패키지 app.tutorialspoint.com.notifyme;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class NotificationBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(컨텍스트 컨텍스트, 의도 의도) { 문자열 작업 =intent.getAction(); if (action.equals( "notification_cancelled" )) { 토스트. makeText (context, "알림 제거됨", Toast.LENGTH_SHORT ).show(); } }}

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

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

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

Android에서 Notification.deleteIntent를 사용하는 방법은 무엇입니까?

Android에서 Notification.deleteIntent를 사용하는 방법은 무엇입니까?