标题翻译
Native Android: Invoke notification when application crash
问题
我希望我的应用在发生崩溃时通知用户。
build.gradle(:app)
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
我所做的事情:
在 onCreate()
上调用 createNotificationChannel()
在 onStop()
和 onDestroy()
上调用 createNotification()
createNotification() 和 createNotificationChannel()
@RequiresApi(api = Build.VERSION_CODES.O)
public Notification.Builder createNotification() {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(this, "screenomics_id")
.setSmallIcon(R.drawable.switch_thumb)
.setContentTitle("Screenomics app has stopped!")
.setContentText("If this is unintentional, please restart the application.")
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
return builder;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "screenomics";
String description = "screenomics description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("screenomics_id", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
onDestroy()
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onDestroy() {
super.onDestroy();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(0, createNotification().build());
}
mHandler.removeCallbacksAndMessages(null);
}
onStop()
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onStop() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
try {
stopRecording();
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
if (notificationManager != null) {
notificationManager.notify(0, createNotification().build());
}
}
}
英文翻译
I would like my app to notify the user if the application has crashed.
build.gradle(:app)
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
Things I did:
invoking createNotificationChannel()
on onCreate()
calling createNotification()
on onStop()
and onDestroy()
createNotification() and createNotificationChannel()
@RequiresApi(api = Build.VERSION_CODES.O)
public Notification.Builder createNotification() {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(this, "screenomics_id")
.setSmallIcon(R.drawable.switch_thumb)
.setContentTitle("Screenomics app has stopped!")
.setContentText("If this is unintentional, please restart the application.")
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
return builder;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "screenomics";
String description = "screenomics description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("screenomics_id", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
onDestroy()
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onDestroy() {
super.onDestroy();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(0, createNotification().build());
}
mHandler.removeCallbacksAndMessages(null);
}
onStop()
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onStop() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
try {
stopRecording();
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
if (notificationManager != null) {
notificationManager.notify(0, createNotification().build());
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论