针对安卓8.0对第一行代码的服务的最佳实践进行部分更改

AndroidManifest.xml文件,权限申请部分

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

build.gradle内的dependencies里添加依赖

implementation 'com.squareup.okhttp3:okhttp:3.14.1'

同时在android内添加

	compileOptions{
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

DownloadService.java部分,使通知适配8.0

	private Notification getNotification(String title, int progress) {
        String channelId = "1";
        String channelName = "Mychannel";
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, channelName,
                    NotificationManager.IMPORTANCE_HIGH);
            getNotificationManager().createNotificationChannel(channel);// 别忘记用manager注册channel
            builder.setSmallIcon(R.drawable.ic_launcher_background);
        } else {
            builder.setSmallIcon(R.mipmap.ic_launcher);
        }
        builder.setContentTitle(title);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setContentIntent(pi);
        if (progress >= 0) {
                builder.setContentText(progress + "%");
                builder.setProgress(100, progress, false);// 第三个参数表示是否使用模糊进度条
        }
        return builder.build();
    }

MainActivity.java部分

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
        Button startDownload = (Button) findViewById(R.id.start_download);
        Button pauseDownload = (Button) findViewById(R.id.pause_download);
        Button cancelDownload = (Button) findViewById(R.id.cancel_download);
        startDownload.setOnClickListener(this);
        pauseDownload.setOnClickListener(this);
        cancelDownload.setOnClickListener(this);
        Intent intent = new Intent(this, DownloadService.class);
        //针对8.0新增的前台服务的startForegroundService()方法进行兼容处理
        if (Build.VERSION.SDK_INT >= 26) {
            startForegroundService(intent);
        } else {
            startService(intent);
        }
        bindService(intent, connection, BIND_AUTO_CREATE);
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission
                .WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]
                    {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }

8.0模拟器上运行结果如图
针对安卓8.0对第一行代码的服务的最佳实践进行部分更改_第1张图片

你可能感兴趣的:(针对安卓8.0对第一行代码的服务的最佳实践进行部分更改)