将我的项目升级到Android O后
buildToolsVersion "26.0.1"
Run Code Online (Sandbox Code Playgroud)
Android Studio中的Lint显示以下通知构建器方法的已弃用警告:
new NotificationCompat.Builder(context)
Run Code Online (Sandbox Code Playgroud)
问题是: Android开发人员更新了描述NotificationChannel的文档以支持Android O中的通知,并为我们提供了一个代码段,但使用了相同的弃用警告:
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setChannelId(CHANNEL_ID)
.build();
Run Code Online (Sandbox Code Playgroud)
我的问题:是否还有其他解决方案可用于构建通知,并且仍然支持Android O?
我找到的解决方案是将通道ID作为Notification.Builder构造函数中的参数传递.但是这种解决方案并不完全可以重复使用.
new Notification.Builder(MainActivity.this, "channel_id")
Run Code Online (Sandbox Code Playgroud) 我正在创建一个应用程序,用户可以根据GPS位置设置警报.我只想在任何时候激活1个警报.因此,当用户设置第二个警报时,我想取消第一个警报的通知(然后为第二个警报设置新的通知).
现在,我的通知继续堆叠(因为我无法删除它们,所以它们都是活动的).这是我的代码,我试图删除警报和通知:
// Stop the location alarm activity
Intent intentAlarmService_delete = new Intent(v.getContext(), AlarmService.class);
stopService(intentAlarmService_delete); // I think this calls onDestroy() in AlarmService class ...
mNtf = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNtf.cancelAll();
Intent alarmIntent2 = new Intent(getApplicationContext(), OneTimeAlarmReceiver.class);
PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1,
alarmIntent2, PendingIntent.FLAG_CANCEL_CURRENT);
pendingIntentAlarm.cancel();
Run Code Online (Sandbox Code Playgroud)
这是我的AlarmService.class中的onDestroy()函数(我不确定何时调用它...)
public void onDestroy(){
super.onDestroy();
mNtf.cancel(NOTIFICATION_ID1);
Intent alarmIntent = new Intent(getApplicationContext(), OneTimeAlarmReceiver.class);
PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1,
alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
pendingIntentAlarm.cancel();
Intent intentAlarmService = new Intent(getApplicationContext(), AlarmService.class);
stopService(intentAlarmService);
mNtf.cancel(NOTIFICATION_ID1);
mNtf.cancelAll();
}
Run Code Online (Sandbox Code Playgroud)
然后,这就是我设置新警报和通知的方式:
Intent intentAlarmService2 = new Intent(v.getContext(), …Run Code Online (Sandbox Code Playgroud) 通过使用下面的代码,我可以通过全屏隐藏通知栏
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"
Run Code Online (Sandbox Code Playgroud)
要么
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Run Code Online (Sandbox Code Playgroud)
但我想要做的是完全禁用状态栏.我正处于所谓的"自助服务终端模式",我想确保用户不能从顶部挡板上滑下手指.上述两种解决方案都可以隐藏通知栏,但它不适用于在应用程序中完全禁用它.
这可能吗?