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 c1ee04b..39e4b1c 100644 --- a/library/src/main/java/me/shenfan/updateapp/UpdateService.java +++ b/library/src/main/java/me/shenfan/updateapp/UpdateService.java @@ -20,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; @@ -94,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; @@ -277,7 +277,7 @@ private void sendLocalBroadcast(int status, int progress) { @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"; @@ -302,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, "%"));