Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

จะกำหนดเวลาการแจ้งเตือนใน Android ได้อย่างไร


ตัวอย่างนี้สาธิตวิธีกำหนดเวลาการแจ้งเตือนใน Android

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml

<ก่อน>

ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน res/menu/main_menu.xml

<ก่อน><เมนู xmlns:android ="https://schema.android.com/apk/res/android" xmlns:app ="https://schema.android .com/apk/res-auto"> <รายการ android :id ="@+id/action_30" แอป :showAsAction ="ไม่เคย" android :title ="30 วินาที" />

ขั้นตอนที่ 4 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity

แพ็คเกจ app.tutorialspoint.com.notifyme;นำเข้า android.app.AlarmManager;นำเข้า android.app.Notification;นำเข้า android.app.NotificationManager;นำเข้า android.app.PendingIntent;นำเข้า android.content.Context;นำเข้า android เนื้อหาเจตนา นำเข้า android.os.Bundle นำเข้า android.os.SystemClock นำเข้า android.support.v4.app.NotificationCompat นำเข้า android.support.v7.app.AppCompatActivity นำเข้า android.view.เมนู นำเข้า android view.MenuItem;คลาสสาธารณะ MainActivity ขยาย AppCompatActivity { สตริงสุดท้ายแบบคงที่สาธารณะ NOTIFICATION_CHANNEL_ID ="10001"; สตริงคงที่ส่วนตัวสุดท้าย default_notification_channel_id ="default"; @Override ป้องกันโมฆะ onCreate (Bundle saveInstanceState) { super .onCreate (savedInstanceState); setContentView(R.layout. activity_main ); } @Override บูลีนสาธารณะ onCreateOptionsMenu (เมนูเมนู) {// ขยายเมนู; สิ่งนี้จะเพิ่มรายการลงในแถบการดำเนินการหากมีอยู่ getMenuInflater().inflate(R.menu. menu_main , เมนู); คืนค่าจริง; } @แทนที่บูลีนสาธารณะ onOptionsItemSelected (รายการเมนู) { สวิตช์ (item.getItemId ()) { กรณี R.id action_5 :scheduleNotification(getNotification( "ล่าช้า 5 วินาที" ) , 5000 ); คืนค่าจริง; กรณี ร.ด. action_10 :scheduleNotification(getNotification( "ล่าช้า 10 วินาที" ) , 10000 ); คืนค่าจริง; กรณี ร.ด. action_30 :scheduleNotification(getNotification( "หน่วงเวลา 30 วินาที" ) , 30000 ); คืนค่าจริง; default :return super .onOptionsItemSelected(item); } } กำหนดการเป็นโมฆะส่วนตัว (การแจ้งเตือน ความล่าช้า int) { เจตนา notificationIntent =เจตนาใหม่ (สิ่งนี้ คลาส MyNotificationPublisher. ); alertIntent.putExtra(MyNotificationPublisher. NOTIFICATION_ID , 1 ); alertIntent.putExtra(MyNotificationPublisher. NOTIFICATION , การแจ้งเตือน); ความตั้งใจที่รอดำเนินการ =ความตั้งใจที่รอดำเนินการ getBroadcast ( นี่, 0, notificationIntent , PendingIntent. FLAG_UPDATE_CURRENT ); อนาคตอันยาวนานInMillis =SystemClock elapsedRealtime () + ล่าช้า; AlarmManager alarmManager =(AlarmManager) getSystemService (บริบท ALARM_SERVICE ); ยืนยัน alarmManager !=null; alarmManager.set (AlarmManager. ELAPSED_REALTIME_WAKEUP , futureInMillis , pendingIntent ); } รับการแจ้งเตือนส่วนตัว (เนื้อหาสตริง) { ตัวสร้าง NotificationCompat.Builder =ใหม่ NotificationCompat.Builder (นี้ default_notification_channel_id ); builder.setContentTitle ( "การแจ้งเตือนตามกำหนดเวลา" ); builder.setContentText(เนื้อหา); builder.setSmallIcon(R.drawable. ic_launcher_foreground ); builder.setAutoCancel (จริง); builder.setChannelId ( NOTIFICATION_CHANNEL_ID ); ส่งคืน builder.build(); }}

ขั้นตอนที่ 5 − เพิ่มรหัสต่อไปนี้ใน src/MyNotificationPublisher

แพ็คเกจ app.tutorialspoint.com.notifyme;นำเข้า android.app.Notification นำเข้า android.app.NotificationChannel นำเข้า android.app.NotificationManager นำเข้า android.content.BroadcastReceiver นำเข้า android.content.Context นำเข้า android content.Intent นำเข้า app.tutorialspoint.com.notifyme.MainActivity แบบคงที่ NOTIFICATION_CHANNEL_ID คลาสสาธารณะ MyNotificationPublisher ขยาย BroadcastReceiver { สตริงสแตติกสาธารณะ NOTIFICATION_ID ="notification-id"; การแจ้งเตือนสตริงแบบคงที่สาธารณะ ="การแจ้งเตือน"; โมฆะสาธารณะ onReceive (บริบทบริบท เจตนาเจตนา) { NotificationManager notificationManager =(NotificationManager) context.getSystemService (Context. NOTIFICATION_SERVICE ); การแจ้งเตือน =Intent.getParcelableExtra (การแจ้งเตือน); if (android.os.Build.VERSION. SDK_INT>=android.os.Build.VERSION_CODES. O ) { ความสำคัญจริง =NotificationManager IMPORTANCE_HIGH; NotificationChannel alertChannel =new NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , ความสำคัญ); ยืนยันการแจ้งเตือนผู้จัดการ !=null; alertManager.createNotificationChannel(ช่องแจ้งเตือน); } int id =เจตนา.getIntExtra( NOTIFICATION_ID , 0 ); ยืนยันการแจ้งเตือนผู้จัดการ !=null; alertManager.notify(id , การแจ้งเตือน); }}

ขั้นตอนที่ 6 − เพิ่มรหัสต่อไปนี้ใน AndroidManifest.xml

<ก่อน><แสดง xmlns:android ="https://schema.android.com/apk/res/android" package ="app.tutorialspoint.com.notifyme"> <หมวดหมู่ android :name ="android.intent.category.LAUNCHER" />

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

จะกำหนดเวลาการแจ้งเตือนใน Android ได้อย่างไร

จะกำหนดเวลาการแจ้งเตือนใน Android ได้อย่างไร