android 9.0上使用前台服务,需要添加权限

 

重写onCreate

public void onCreate(){        super.onCreate();        Log.d("MyService","onCreate executed");        Intent intent = new Intent(this,MainActivity.class);        PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);        Notification notification = null;        NotificationManager manager = (NotificationManager) getApplication().getSystemService(NOTIFICATION_SERVICE);        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {            Uri mUri = Settings.System.DEFAULT_NOTIFICATION_URI;            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ONE_ID, "driver", NotificationManager.IMPORTANCE_LOW);//CHANNEL_ONE_ID自定义            mChannel.setDescription("description");            mChannel.setSound(mUri, Notification.AUDIO_ATTRIBUTES_DEFAULT);            manager.createNotificationChannel(mChannel);            notification = new Notification.Builder(this, CHANNEL_ONE_ID)                    .setChannelId(CHANNEL_ONE_ID)                    .setSmallIcon(R.mipmap.ic_launcher)                    .setContentTitle(getString(R.string.app_name))                    .setContentText("hello world")                    .setContentIntent(pi)                    .build();        } else {            // 提升应用权限            notification = new Notification.Builder(this)                    .setSmallIcon(R.mipmap.ic_launcher)                    .setContentTitle(getString(R.string.app_name))                    .setContentText("hello world")                    .setContentIntent(pi)                    .build();        }        notification.flags = Notification.FLAG_ONGOING_EVENT;        notification.flags |= Notification.FLAG_NO_CLEAR;        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;        startForeground(10000, notification);    }

更多相关文章

  1. 重写gallery 的 BaseAdapter
  2. Android SELinux开发入门指南之正确姿势解决访问data目录权限问
  3. [Android Pro] android root权限破解分析
  4. 轻量级Android6.0动态权限解决方案

随机推荐

  1. ActivityManagerService架构剖析开篇
  2. android builde.gradle 动态配置域名
  3. 下载Android(安卓)ADT时找不到org.eclips
  4. Android中Junit测试、XML的序列化与解析
  5. Android(安卓)中CollapsingToolbarLayout
  6. 《第一行代码》第一章
  7. Android(安卓)如何让你的App分享给别人
  8. android应用程序--计算器
  9. Android平台中各类恶意软件及病毒概览
  10. Android(安卓)Banner轮播控件