|
@@ -1,7 +1,7 @@
|
|
|
package me.yoqi.mobile.tool;
|
|
package me.yoqi.mobile.tool;
|
|
|
|
|
|
|
|
-import android.annotation.TargetApi;
|
|
|
|
|
import android.app.Notification;
|
|
import android.app.Notification;
|
|
|
|
|
+import android.app.NotificationChannel;
|
|
|
import android.app.NotificationManager;
|
|
import android.app.NotificationManager;
|
|
|
import android.app.PendingIntent;
|
|
import android.app.PendingIntent;
|
|
|
import android.app.Service;
|
|
import android.app.Service;
|
|
@@ -16,6 +16,8 @@ public class ServiceTool extends Service {
|
|
|
|
|
|
|
|
private NotificationManager mNF;
|
|
private NotificationManager mNF;
|
|
|
public static String AUTO_START = "com.ldci.android.t56.mobile.safe.AUTO_START";
|
|
public static String AUTO_START = "com.ldci.android.t56.mobile.safe.AUTO_START";
|
|
|
|
|
+ private static final String CHANNEL_ID = "365_mobile_secretary_channel";
|
|
|
|
|
+ private static final int NOTIFICATION_ID = R.string.app_name;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public IBinder onBind(Intent arg0) {
|
|
public IBinder onBind(Intent arg0) {
|
|
@@ -25,51 +27,76 @@ public class ServiceTool extends Service {
|
|
|
@Override
|
|
@Override
|
|
|
public void onCreate() {
|
|
public void onCreate() {
|
|
|
super.onCreate();
|
|
super.onCreate();
|
|
|
|
|
+ createNotificationChannel();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // public static int serviceInt = 0;
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public void onStart(Intent intent, int startId) {
|
|
|
|
|
- super.onStart(intent, startId);
|
|
|
|
|
-// Toast.makeText(this, "开启服务,发出通知", Toast.LENGTH_LONG).show();
|
|
|
|
|
-// new Thread(new Runnable(){
|
|
|
|
|
-// public void run(){
|
|
|
|
|
-// while(true){
|
|
|
|
|
-// try {
|
|
|
|
|
-// Thread.sleep(1000);
|
|
|
|
|
-// serviceInt = 0;
|
|
|
|
|
-// } catch (InterruptedException e) {
|
|
|
|
|
-// e.printStackTrace();
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// }).start();
|
|
|
|
|
|
|
+ public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
autoStartNotification();
|
|
autoStartNotification();
|
|
|
|
|
+ return START_STICKY;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void onDestroy() {
|
|
public void onDestroy() {
|
|
|
super.onDestroy();
|
|
super.onDestroy();
|
|
|
-// Toast.makeText(this, "停止服务,取消通知", Toast.LENGTH_LONG).show();
|
|
|
|
|
- mNF.cancel(R.string.app_name);
|
|
|
|
|
|
|
+ if (mNF != null) {
|
|
|
|
|
+ mNF.cancel(NOTIFICATION_ID);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void createNotificationChannel() {
|
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
+ CharSequence name = getString(R.string.app_name);
|
|
|
|
|
+ String description = "365手机秘书服务通知";
|
|
|
|
|
+ int importance = NotificationManager.IMPORTANCE_LOW;
|
|
|
|
|
+ NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
|
|
|
|
|
+ channel.setDescription(description);
|
|
|
|
|
+
|
|
|
|
|
+ if (mNF == null) {
|
|
|
|
|
+ mNF = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
|
|
|
|
+ }
|
|
|
|
|
+ mNF.createNotificationChannel(channel);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
|
|
|
|
private void autoStartNotification() {
|
|
private void autoStartNotification() {
|
|
|
- Notification mNotification = new Notification(R.drawable.app_logo, "365手机秘书", System.currentTimeMillis());
|
|
|
|
|
Intent intent = new Intent(this, MainActivity.class);
|
|
Intent intent = new Intent(this, MainActivity.class);
|
|
|
intent.setAction(AUTO_START);
|
|
intent.setAction(AUTO_START);
|
|
|
intent.putExtra("auto_start", "boot_completed");
|
|
intent.putExtra("auto_start", "boot_completed");
|
|
|
- PendingIntent mPI = PendingIntent.getActivity(this, 0, intent, 0);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ int pendingIntentFlags;
|
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
|
+ pendingIntentFlags = PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ PendingIntent mPI = PendingIntent.getActivity(this, 0, intent, pendingIntentFlags);
|
|
|
|
|
|
|
|
-// mNotification.setLatestEventInfo(this, "365手机秘书", "成功开机启动", mPI);
|
|
|
|
|
-// Notification.Builder builder = new Notification.Builder(MainActivity.this);
|
|
|
|
|
-// builder.setAutoCancel(true);
|
|
|
|
|
|
|
+ Notification notification;
|
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
+ notification = new Notification.Builder(this, CHANNEL_ID)
|
|
|
|
|
+ .setContentTitle("365手机秘书")
|
|
|
|
|
+ .setContentText("成功开机启动")
|
|
|
|
|
+ .setSmallIcon(R.drawable.app_logo)
|
|
|
|
|
+ .setContentIntent(mPI)
|
|
|
|
|
+ .setAutoCancel(true)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ notification = new Notification.Builder(this)
|
|
|
|
|
+ .setContentTitle("365手机秘书")
|
|
|
|
|
+ .setContentText("成功开机启动")
|
|
|
|
|
+ .setSmallIcon(R.drawable.app_logo)
|
|
|
|
|
+ .setContentIntent(mPI)
|
|
|
|
|
+ .setAutoCancel(true)
|
|
|
|
|
+ .setWhen(System.currentTimeMillis())
|
|
|
|
|
+ .getNotification();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (null == mNF) {
|
|
|
|
|
|
|
+ if (mNF == null) {
|
|
|
mNF = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
|
mNF = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
|
|
}
|
|
}
|
|
|
- mNF.notify(R.string.app_name, mNotification);
|
|
|
|
|
|
|
+ mNF.notify(NOTIFICATION_ID, notification);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|