学习安卓中遇到的问题记录
程序员文章站
2022-05-31 16:09:32
...
1 获取string.xml文件里的值(已解决)
String itemName=(String)MainActivity.this.getResources().getText(R.string.item_name);
如果直接引用R.string.item_name,返回的结果是一个长整数
2 Android8.0及以上 收不到通知Notification(未解决)
下面代码是IntentService类的子类的onHandleIntent方法中实现的
// 自定义通知ID
int NOTIFICATION_ID = 1;
String channelID = "channel_1";
String channelName = "channel_name_1";
String question = "What is the secret of comedy?";
String answer = "Timing!";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.setContentTitle(question)
.setChannelId(channelID)
.setContentText(answer)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVibrate(new long[] {0, 1000})
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 此处必须兼容android O设备,否则系统版本在O以上可能不展示通知栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(NOTIFICATION_ID, builder.build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
这个判断的代码实现,并没有起到实际效果,真机调试的时候仍然显示不出通知栏