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

Android 알림을 탭할 때 마지막 활동을 여는 방법은 무엇입니까?

<시간/>

이 예는 Android 알림을 탭할 때 마지막 활동을 여는 방법에 대해 보여줍니다.

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

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

 

3단계 − src/MainActivity.java

에 다음 코드 추가
패키지 app.tutorialspoint.com.notifyme;import android.app.NotificationChannel;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android. support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { public static final String NOTIFICATION_CHANNEL_ID ="10001"; 개인 최종 정적 문자열 default_notification_channel_id ="기본"; @Override 보호된 무효 onCreate(SavedInstanceState 번들) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); 버튼 btnCreateNotification =findViewById(R.id.btnCreateNotification ); btnCreateNotification.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent notificationIntent =new Intent(MainActivity. this, MainActivity.class ); notificationIntent.addCategory(Intent. CATEGORY_LAUNCHER); notificationIntent.setAction(Intent . ACTION_MAIN ); notificationIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP | Intent. FLAG_ACTIVITY_SINGLE_TOP ); PendingIntent resultIntent =PendingIntent.getActivity (MainActivity. this, 0 ,notificationIntent ._channel.) ,notificationIntent. ) .setSmallIcon(R.drawable.ic_launcher_foreground ) .setContentTitle( "테스트" ) .setContentIntent(resultIntent) .setStyle( new NotificationCompat.InboxStyle()) .setContentText( "안녕하세요! 이것은 저의 첫 번째 푸시 알림입니다. 양이온" ); NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 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()); } }); }}

4단계 − androidManifest.xml에 다음 코드 추가

<전> <카테고리 Android:name ="android.intent .category.LAUNCHER" />

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

Android 알림을 탭할 때 마지막 활동을 여는 방법은 무엇입니까?