开发者社区 问答 正文

我自己的Android应用导致手机崩溃

我正在尝试学习如何在Android上触发通知。该应用程序可以在虚拟设备上运行,但是当我在实际的三星Galaxy S7(Android版本:8.1.0)上尝试使用该应用程序时,每次按下通知按钮时都会显示一条消息,提示“系统用户界面已停止”,该手机会冻结手机我的手机无限期无响应,需要重启。

public class MainActivity extends AppCompatActivity {

    String CHANNEL1_ID = "Channel 1 id";
    String CHANNEL1_NAME = "Channel 1";
    String channel1_description = "Channel 1 description";
    String textTitle = "This is a title.";
    String textContent = "Some content.";
    int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        final Context context = this;

        createNotificationChannel();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL1_ID);
                builder.setSmallIcon(R.drawable.notification_image);
                builder.setContentTitle(textTitle);
                builder.setContentText(textContent);
                builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

                // notificationId is a unique int for each notification that you must define
                notificationManager.notify(count, builder.build());

                count++;

                Log.d("TAG " + count, "onClick: ");

            }

        });

    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String name = CHANNEL1_NAME;
            String description = channel1_description;
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL1_ID, name, importance);
            channel.setDescription(description);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

}

问题来源:Stack Overflow

展开
收起
montos 2020-03-25 19:19:26 539 分享 版权
阿里云 AI 助理回答

遇到这个问题,可能是由于以下原因导致的:

  1. 通知渠道(Notification Channel)未正确创建或配置:在Android 8.0及以上版本中,必须通过创建通知渠道来发送通知。你的代码已经包含了创建渠道的部分,但请确保这部分逻辑在运行时能够被执行到,并且没有因为设备特定的问题而失败。检查createNotificationChannel()方法是否在所有目标设备上都能正常工作。

  2. 资源引用错误:你设置了一个小图标R.drawable.notification_image,请确认这个资源文件存在于你的项目中,并且在三星Galaxy S7的设备兼容性上没有问题。有时候,不同设备对图像尺寸和格式的要求可能有所不同。

  3. 内存泄漏或无限制递增的通知ID:在你的onClick方法里,count变量用于作为通知的ID,但是你似乎忘记递增它了。这会导致每次点击按钮时尝试使用相同的ID发送通知,而在某些情况下可能会引发问题。你应该在每次调用notify方法后增加count的值,例如 count++;

  4. 日志打印问题:你的日志打印语句Log.d("TAG " count, "onClick: ");格式不正确,应该修正为Log.d("TAG", "onClick: " + count);以避免潜在的异常。

  5. 系统兼容性问题:尽管你的代码看起来是按照现代Android实践编写的,但有时特定设备上的系统定制可能会引入未知的兼容性问题。考虑检查Samsung Galaxy S7是否有已知的与通知相关的bug,或者尝试在其他相同Android版本的设备上测试应用,看是否复现该问题。

  6. 权限问题:虽然通常情况下,发送通知不需要特别的权限,但在极少数设备或定制ROM中,可能存在额外的限制。确保应用具有显示通知的必要权限,并且这些权限没有被用户手动禁用。

建议逐一排查上述点,特别是修复count递增的遗漏,并确保所有资源正确引用。如果问题依旧,尝试在开发者选项中启用更多日志输出,或者使用如Logcat等工具捕获更详细的错误信息,以便进一步诊断问题所在。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答