diff --git a/README-zh.md b/README-zh.md index f77682f..83efab7 100644 --- a/README-zh.md +++ b/README-zh.md @@ -56,13 +56,10 @@ allprojects { } } -`SNAPSHOT` -dependencies { - compile 'com.github.yaming116:UpdateApp:1.0.4-SNAPSHOT' -} + dependencies { - compile 'com.github.yaming116:UpdateApp:1.0.4' + compile 'com.github.yaming116:UpdateApp:1.0.5' } ``` diff --git a/app/build.gradle b/app/build.gradle index adf7b25..34a5064 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion "25.0.3" + compileSdkVersion 27 + buildToolsVersion "27.0.1" defaultConfig { applicationId "me.shenfan.updateapp.sample" minSdkVersion 14 - targetSdkVersion 25 + targetSdkVersion 27 versionCode 1 versionName "1.0" } diff --git a/build.gradle b/build.gradle index 37abb1e..44e1d9a 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,7 @@ buildscript { allprojects { repositories { maven { url "https://www.jitpack.io" } + maven {url "https://dl.google.com/dl/android/maven2/"} jcenter() } } diff --git a/library/build.gradle b/library/build.gradle index 3a7250f..0554b60 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 25 - buildToolsVersion "25.0.3" + compileSdkVersion 26 + buildToolsVersion "25.0.1" defaultConfig { minSdkVersion 14 - targetSdkVersion 25 + targetSdkVersion 26 versionCode 3 versionName "1.0.4" } @@ -25,5 +25,5 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'com.android.support:support-core-utils:25.3.1' + provided 'com.android.support:support-core-utils:25.3.1' } diff --git a/library/src/main/java/android/support/v4/app/LocalNotificationCompat.java b/library/src/main/java/android/support/v4/app/LocalNotificationCompat.java new file mode 100644 index 0000000..638f995 --- /dev/null +++ b/library/src/main/java/android/support/v4/app/LocalNotificationCompat.java @@ -0,0 +1,62 @@ +package android.support.v4.app; + +import android.app.Notification; +import android.content.Context; +import android.support.annotation.NonNull; + +import java.util.ArrayList; + +/** + * Created by ucmed on 2018/3/8. + */ +public class LocalNotificationCompat extends NotificationCompat { + public static class Builder extends NotificationCompat.Builder{ + String mChannelId; + /** + * Constructor. + * + * Automatically sets the when field to {@link System#currentTimeMillis() + * System.currentTimeMillis()} and the audio stream to the + * {@link Notification#STREAM_DEFAULT}. + * + * @param context A {@link Context} that will be used to construct the + * RemoteViews. The Context will not be held past the lifetime of this + * Builder object. + * @param channelId The constructed Notification will be posted on this + * NotificationChannel. + */ + public Builder(@NonNull Context context, @NonNull String channelId) { + super(context); + mContext = context; + mChannelId = channelId; + + // Set defaults to match the defaults of a Notification + mNotification.when = System.currentTimeMillis(); + mNotification.audioStreamType = Notification.STREAM_DEFAULT; + mPriority = PRIORITY_DEFAULT; + mPeople = new ArrayList(); + } + + /** + * @deprecated use {@link #LocalNotificationCompat.Builder(Context,String)} instead. + * All posted Notifications must specify a NotificationChannel Id. + */ + @Deprecated + public Builder(Context context) { + this(context, null); + } + + + /** + * Specifies the channel the notification should be delivered on. + * + * No-op on versions prior to {@link android.os.Build.VERSION_CODES#O} . + */ + public Builder setChannelId(@NonNull String channelId) { + mChannelId = channelId; + return this; + } + } + + +} diff --git a/library/src/main/java/me/shenfan/updateapp/UpdateService.java b/library/src/main/java/me/shenfan/updateapp/UpdateService.java index 65371b4..39e4b1c 100644 --- a/library/src/main/java/me/shenfan/updateapp/UpdateService.java +++ b/library/src/main/java/me/shenfan/updateapp/UpdateService.java @@ -1,6 +1,8 @@ package me.shenfan.updateapp; +import android.annotation.TargetApi; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; @@ -10,6 +12,7 @@ import android.content.pm.PackageManager; import android.content.pm.ProviderInfo; import android.graphics.BitmapFactory; +import android.graphics.Color; import android.net.Uri; import android.os.AsyncTask; import android.os.Binder; @@ -17,7 +20,7 @@ import android.os.Environment; import android.os.IBinder; import android.support.annotation.Nullable; -import android.support.v4.app.NotificationCompat; +import android.support.v4.app.LocalNotificationCompat; import android.support.v4.content.FileProvider; import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; @@ -91,7 +94,7 @@ public void setUpdateProgressListener(UpdateProgressListener listener) { private boolean startDownload;//开始下载 private int lastProgressNumber; - private NotificationCompat.Builder builder; + private LocalNotificationCompat.Builder builder; private NotificationManager manager; private int notifyId; private String appName; @@ -271,9 +274,23 @@ private void sendLocalBroadcast(int status, int progress) { localBroadcastManager.sendBroadcast(localIntent); } + @TargetApi(26) private void buildNotification() { manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - builder = new NotificationCompat.Builder(this); + builder = new LocalNotificationCompat.Builder(this); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + CharSequence name = "update_channel"; + String Description = "zhuojian update channel"; + int importance = NotificationManager.IMPORTANCE_HIGH; + NotificationChannel mChannel = new NotificationChannel(String.valueOf(notifyId), name, importance); + mChannel.setDescription(Description); + mChannel.enableLights(true); + mChannel.setLightColor(Color.BLUE); + mChannel.enableVibration(true); + mChannel.setShowBadge(false); + manager.createNotificationChannel(mChannel); + builder.setChannelId(String.valueOf(notifyId)); + } builder.setContentTitle(getString(R.string.update_app_model_prepare, appName)) .setWhen(System.currentTimeMillis()) .setProgress(100, 1, false) @@ -285,6 +302,7 @@ private void buildNotification() { manager.notify(notifyId, builder.build()); } + private void start() { builder.setContentTitle(appName); builder.setContentText(getString(R.string.update_app_model_progress, 1, "%"));