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

จะทำให้การแจ้งเตือนแถบสถานะ Android ยังคงอยู่ระหว่างการรีบูตโทรศัพท์ได้อย่างไร


ตัวอย่างนี้สาธิตวิธีทำให้การแจ้งเตือนแถบสถานะ Android ยังคงอยู่ในระหว่างการรีบูตโทรศัพท์

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

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

<ก่อน>

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

แพ็คเกจ app.tutorialspoint.com.notifyme;นำเข้า android.os.Bundle;นำเข้า android.support.v7.app.AppCompatActivity;คลาสสาธารณะ MainActivity ขยาย AppCompatActivity { @Override ป้องกันโมฆะ onCreate (บันเดิลที่บันทึกไว้InstanceState) { super .onCreate ( saveInstanceState); setContentView(R.layout. activity_main ); }}

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

แพ็คเกจ app.tutorialspoint.com.notifyme;นำเข้า android.annotation SuppressLint;นำเข้า android.app.Notification;นำเข้า android.app.NotificationChannel นำเข้า android.app.NotificationManager นำเข้า android.content.BroadcastReceiver นำเข้า android.content.Context นำเข้า android.content.Intent นำเข้า android.support.v4 .app.NotificationCompat นำเข้า android.util.Log คลาสสาธารณะ USBStateReceiver ขยาย BroadcastReceiver { สตริงสุดท้ายแบบคงที่สาธารณะ NOTIFICATION_CHANNEL_ID ="10001"; สตริงคงที่ส่วนตัวสุดท้าย default_notification_channel_id ="default"; บูลีนเชื่อมต่อ =จริง; @SuppressLint ( "UnsafeProtectedBroadcastReceiver" ) @ แทนที่โมฆะสาธารณะ onReceive (บริบทบริบทเจตนาเจตนา) { NotificationCompat.Builder builder =ใหม่ NotificationCompat.Builder (บริบท default_notification_channel_id); builder.setContentTitle("USB - การแจ้งเตือน" ); การกระทำของสตริง =Intent.getAction(); บันทึก. e ("USB" , การกระทำ); ยืนยันการกระทำ !=null; builder.setContentText( "เชื่อมต่อแล้ว" ); builder.setSmallIcon(R.drawable. ic_launcher_foreground ); builder.setAutoCancel (จริง); builder.setChannelId ( NOTIFICATION_CHANNEL_ID ); การแจ้งเตือน =builder.build(); NotificationManager notificationManager =(NotificationManager) context.getSystemService (Context. NOTIFICATION_SERVICE ); 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(ช่องแจ้งเตือน); } ยืนยัน notificationManager !=null; ถ้า ( เชื่อมต่อ ) { alertManager.notify (1 , การแจ้งเตือน); เชื่อมต่อ =เท็จ; } อื่น ๆ { alertManager.cancel( 1 ); เชื่อมต่อ =จริง; }}

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

<ก่อน> <หมวดหมู่ android :name ="android.intent.category.LAUNCHER" /> <เครื่องรับ android :name =".USBStateReceiver">

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

จะทำให้การแจ้งเตือนแถบสถานะ Android ยังคงอยู่ระหว่างการรีบูตโทรศัพท์ได้อย่างไร