我正在尝试学习如何在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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。