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

จะล้างการแจ้งเตือน Android หากกิจกรรมขัดข้องได้อย่างไร


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

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

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

<ก่อน> <ปุ่ม android :id ="@+id/btnCreateNotification" android:layout_width ="wrap_content" android :layout_height ="wrap_content" android :layout_alignParentStart ="จริง" android :layout_alignParentEnd ="จริง" android :layout_centerInParent ="จริง" android :text ="สร้างการแจ้งเตือน" />

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

แพ็คเกจ app.tutorialspoint.com.notifyme;นำเข้า android.app.NotificationChannel;นำเข้า android.app.NotificationManager;นำเข้า android.app.PendingIntent;นำเข้า android.content.Intent;นำเข้า android.os.Bundle;นำเข้า android support.v4.app.NotificationCompat; นำเข้า android.support.v7.app.AppCompatActivity; นำเข้า android.view.View; MainActivity คลาสสาธารณะขยาย AppCompatActivity { สตริงสุดท้ายแบบคงที่สาธารณะ NOTIFICATION_CHANNEL_ID ="10001"; สตริงคงที่ส่วนตัวสุดท้าย default_notification_channel_id ="default"; @Override ป้องกันโมฆะ onCreate (Bundle saveInstanceState) { super .onCreate (savedInstanceState); setContentView(R.layout. activity_main ); } โมฆะสาธารณะ createNotification (ดูมุมมอง) { Intent notificationIntent =เจตนาใหม่ (getApplicationContext () , MainActivity. class ); alertIntent.putExtra( "จากการแจ้งเตือน" จริง ); alertIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP | Intent. FLAG_ACTIVITY_SINGLE_TOP ); ความตั้งใจที่รอดำเนินการ =ความตั้งใจที่รอดำเนินการ getActivity ( นี่ , 0 , การแจ้งเจตนา , 0 ); NotificationManager mNotificationManager =(NotificationManager) getSystemService( NOTIFICATION_SERVICE ); NotificationCompat.Builder mBuilder =ใหม่ NotificationCompat.Builder(MainActivity. this, default_notification_channel_id ); mBuilder.setContentTitle( "การแจ้งเตือนของฉัน" ); mBuilder.setContentIntent(รอดำเนินการ); mBuilder.setColor(getColor(R.color. colorPrimary )); mBuilder.setContentText( "ตัวอย่างบริการผู้ฟังการแจ้งเตือน" ); mBuilder.setSmallIcon(R.drawable. ic_launcher_foreground ); mBuilder.setAutoCancel( จริง ); 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" , ความสำคัญ); mBuilder.setChannelId ( NOTIFICATION_CHANNEL_ID ); ยืนยัน mNotificationManager !=null; mNotificationManager.createNotificationChannel (ช่องแจ้งเตือน); } ยืนยัน mNotificationManager !=null; mNotificationManager.notify(( int ) ระบบ currentTimeMillis () , mBuilder.build()); }}

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

แพ็คเกจ app.tutorialspoint.com.notifyme;import android.app.NotificationManager;import android.content.Context; CrashHandler คลาสสาธารณะใช้ Thread.UncaughtExceptionHandler { ส่วนตัวคงที่ int สุดท้ายสุดท้าย NOTIFICATION_ID =12345; ส่วนตัว Thread.UncaughtExceptionHandler defaultUEH; NotificationManager ส่วนตัว notificationManager; CrashHandler สาธารณะ (บริบทบริบท) { this . defaultUEH =เธรด getDefaultUncaughtExceptionHandler (); alertManager =(NotificationManager) บริบท.getSystemService (บริบท. NOTIFICATION_SERVICE ); } โมฆะสาธารณะ uncaughtException (Thread t , Throwable e) { ถ้า ( notificationManager !=null ) { ลอง { alertManager .cancel ( NOTIFICATION_ID ); } จับ (โยนได้อดีต) { ex.printStackTrace (); } } notificationManager =null; defaultUEH .uncaughtException(t , e); }}

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

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