在奥利奥版本中Android应用程序被杀死后,在后台每秒钟运行一次API



我正在尝试构建一个每秒运行一次的Android应用程序,当应用程序关闭或关闭时,它也应该在后台连续运行。当满足API响应条件时,应显示本地通知。。

我已将服务类用于后台任务。除了奥利奥版本(8.1v(之外,它在所有版本中都运行良好

我查看了网站和与之相关的示例,发现在应用程序关闭或关闭后,我们无法在Oreo版本中执行后台任务。

所以我尝试使用startForeground((,但它也不起作用,经过多次尝试,我终于在这里提出了这个问题。

所以请帮我在后台运行API应用程序关闭时。

MainActivty.class

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(this, new Intent(this,MyService.class));
} else {
startService(new Intent(this,MyService.class));
}
}

MyService.class

public class MyService extends Service {
public static final int notify = 3000;  //interval between two services(Here Service run every 5 Minute)
private Handler mHandler = new Handler();   //run on another Thread to avoid crash
private Timer mTimer = null;    //timer handling
@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) {
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
}
//class TimeDisplay for handling task
class TimeDisplay extends TimerTask {
@Override
public void run() {
mHandler.post(new Runnable() {
@Override
public void run() {
new ApiCallAsyncTask().execute(URL);
}
});
}
}
}

在ApiCallAsyncTask类中调用的通知方法

Notification notif;
@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void notification(String Name, String time,String mId,int id){
Intent intent = new Intent(MyService.this, MainActivity.class);
String CHANNEL_ID = String.valueOf(id);
PendingIntent pendingIntent = PendingIntent.getActivity(MyService.this, 100, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, Name, NotificationManager.IMPORTANCE_DEFAULT);
notif = new Notification.Builder(MyService.this)
.setContentIntent(pendingIntent)
.setContentTitle("Reminder")
.setContentText("hello")
.setSmallIcon(R.drawable.logo)
.setOnlyAlertOnce(true)
.setColor(ContextCompat.getColor(MyService.this, R.color.colorPrimaryDark))
.setChannelId(CHANNEL_ID)
.build();
notificationManager.createNotificationChannel(mChannel);
}else {
notif = new Notification.Builder(MyService.this)
.setContentIntent(pendingIntent)
.setContentTitle("Reminder")
.setContentText("hello")
.setSmallIcon(R.drawable.logo)
.setOnlyAlertOnce(true)
.setColor(ContextCompat.getColor(MyService.this, R.color.colorPrimaryDark))
.build();
}
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, notif);
startForeground(1, notif);
}

谢谢。。

您可以使用JobIntentService+AlarmManager(用于调度(或JobScheduler API的组合。

但我强烈建议用Firebase云消息取代您的方法。因此,您将把业务逻辑放在服务器端,并在特殊情况下通知客户端。

相关内容

  • 没有找到相关文章

最新更新