Native Android: 当应用程序崩溃时触发通知

huangapple 未分类评论46阅读模式
标题翻译

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());
                }
            }

        }

huangapple
  • 本文由 发表于 2020年3月16日 12:32:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/60700368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定