Computer >> Máy Tính >  >> Lập trình >> Android

Làm cách nào để mở hoạt động cuối cùng khi nhấn vào thông báo Android?

Ví dụ này minh họa về Cách mở hoạt động gần đây nhất khi nhấn vào thông báo Android

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

     
  Bước 3  - Thêm mã sau vào src / MainActivity.java 

 gói app.tutorialspoint.com.notifyme; nhập android.app.NotificationChannel; nhập android.app.NotificationManager; nhập android.app.PendingIntent; nhập android.content.Context; nhập android.content.Intent; nhập android. support.v4.app.NotificationCompat; nhập android.support.v7.app.AppCompatActivity; nhập android.os.Bundle; nhập android.view.View; nhập android.widget.Button; public class MainActivity mở rộng AppCompatActivity {public static final String NOTIFICATION_CHANNEL_ID ="10001"; private cuối cùng static String default_notification_channel_id ="default"; @Override được bảo vệ void onCreate (Bundle saveInstanceState) {super .onCreate (saveInstanceState); setContentView (R.layout. activity_main); Nút 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. . ACTION_MAIN); notificationIntent.setFlags (Ý định. FLAG_ACTIVITY_CLEAR_TOP | Ý định. FLAG_ACTIVITY_SINGLE_TOP); PendingIntent resultIntent =PendingIntent. GetActivity (MainActivity. This, 0, notificationIntent, 0); NotificationCompat this. ) .setSmallIcon (R.drawable. ic_launcher_foreground) .setContentTitle ("Thử nghiệm") .setContentIntent (resultIntent) .setStyle (mới NotificationCompat.InboxStyle ()) .setContentText ("Xin chào! Đây là thông báo đẩy đầu tiên của tôi cation "); NotificationManager mNotificationManager =(NotificationManager) getSystemService (Ngữ cảnh. NOTIFICATION_SERVICE); if (android.os.Build.VERSION. SDK_INT> =android.os.Build.VERSION_CODES. O) {int important =NotificationManager. TẦM QUAN TRỌNG:CAO; NotificationChannel notificationChannel =new NotificationChannel (NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", tầm quan trọng); mBuilder.setChannelId (NOTIFICATION_CHANNEL_ID); khẳng định mNotificationManager! =null; mNotificationManager.createNotificationChannel (kênh thông báo); } khẳng định mNotificationManager! =null; mNotificationManager.notify ((int) Hệ thống. currentTimeMillis (), mBuilder.build ()); }}); }} 

Bước 4 - Thêm mã sau vào androidManifest.xml

   <ứng dụng android:allowBackup ="true" android:icon ="@ mipmap / ic_launcher" android:label ="@ string / app_name" android:roundIcon ="@ mipmap / ic_launcher_round" android:supportsRtl ="true" android:theme ="@ style / AppTheme">         

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm cách nào để mở hoạt động cuối cùng khi nhấn vào thông báo Android?