杀死我的应用程序时,我正在后台任务中继续检查后台任务。
我想在杀死我的应用程序时保留API中的每个检查数据,如果有任何新数据,它会显示本地推送通知。这段代码适用于棉花糖及以下版本,但 Nougat 及以上版本不起作用,您能帮我吗?
public class MyService extends Service {
public static final int notify = 60*10*1000; //interval between two services(Here Service run every 5 Minute)
private Handler mHandler = new Handler();
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
if (mTimer != null) // Cancel if already existed
mTimer.cancel();
else
mTimer = new Timer(); //recreate new
mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify); //Schedule task
//mTimer.scheduleAtFixedRate(new MyTask(), millisToNextHour(calendar), 60*60*1000, TimeUnit.MILLISECONDS);
}
@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void processFinish(String output) {
if (count20new > count20old) {
// Toast.makeText(MyService.this, "Service is running", Toast.LENGTH_SHORT).show();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
String message = "To see the latest message.";
Intent intent = new Intent(getApplicationContext(), MyService.class);
// PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(getApplicationContext());
Resources res = getApplicationContext().getResources();
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_stat_final)
.setColor(Color.parseColor("#83c3ed"))
.setTicker("Check Latest Notification")
.setContentTitle(res.getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
}
public int getProfilesCount() {
Log.e("my_state","On start command6");
Cursor cursor = db.rawQuery(countQuery, null);
count20new = cursor.getCount();
cursor.close();
return count20new;
}
//class TimeDisplay for handling task
class TimeDisplay extends TimerTask {
@Override
public void run() {
mHandler.post(new Runnable() {
@Override
public void run() {
boolean isConnected = ConnectivityReceiver.isConnected(MyService.this);
if (isConnected) {
Notificationdata();
}
}
});
}
}
public void Notificationdata(){
Log.e("my_state","On start command8");
}
}