Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 本机Android:应用程序崩溃时调用通知_Java_Android - Fatal编程技术网

Java 本机Android:应用程序崩溃时调用通知

Java 本机Android:应用程序崩溃时调用通知,java,android,Java,Android,如果应用程序崩溃,我希望我的应用程序通知用户 build.gradle(:app) 我所做的事情: 调用onCreate() 在onStop()和onDestroy()上调用createNotification() createNotification()和createNotificationChannel() onDestroy() 顶部() 当应用程序崩溃时,运行应用程序中的任何代码都为时已晚。你必须捕获错误/异常,但你应该对你的应用程序进行编程,使其不会发生。请参阅:这听起来可能很奇怪,但

如果应用程序崩溃,我希望我的应用程序通知用户

build.gradle(:app)

我所做的事情: 调用
onCreate()
onStop()
onDestroy()上调用
createNotification()

createNotification()和createNotificationChannel()

onDestroy()

顶部()


当应用程序崩溃时,运行应用程序中的任何代码都为时已晚。你必须捕获错误/异常,但你应该对你的应用程序进行编程,使其不会发生。请参阅:这听起来可能很奇怪,但我的日志没有显示错误所在。我的应用程序预计每隔一段时间在后台运行,经过一些(200-500)迭代后,它将在没有适当警告的情况下突然停止。我认为这可能是因为内存问题,因为每当我运行应用程序,每次打开应用程序,它都会触发(跳过xx帧…),跳过的帧会增加。有没有办法检测到这一点?我怀疑它的“我的应用程序应该在interval的后台运行”,或者你正在做什么,或者Android框架正在运行。如果有一个新的问题,详细说明在后台如何运行(操作系统版本、测试-设备/仿真器、服务如何工作、打瞌睡模式等),你会做得更好。我会检查您是否有服务内存或服务连接泄漏。好的,我会接受您的建议。谢谢
    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'
        }
    }
}
@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);
        }
    }
@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);
    }
@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());
                }
            }

        }