簡體   English   中英

我想創建一個每秒從 api 獲取數據的后台服務,那么如何創建呢?

[英]I Want to create a background service with get data from api in every second so how to create it?

我需要創建一個可以每秒從 API 獲取數據的后台服務,因為我想監視一些數據並將該數據添加到數據庫中。

我嘗試創建如下所示的服務,它工作正常,並且當應用程序被用戶殺死時也可以在后台運行,但問題是當我長時間使用我的應用程序時,應用程序 UI 完全掛起並卡住

  • 我的服務 Class 代碼

     public class MonitoringService extends Service { private static final int NOTIF_ID = 1; private static final String NOTIF_CHANNEL_ID = "Channel_Id"; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { startForeground(); getLiveUpdate(); AlramHendaler alramHendaler = new AlramHendaler(this); alramHendaler.setAlram(); return START_STICKY; } private void getLiveUpdate() { //I use TimerTask for execute thread in background ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); MyTimerTask myTimerTask = new MyTimerTask(() -> { //Here I get data every second from API Call<ArrayList<TempModel>> call = RetrofitClient.getInstance(ResponceData.BASE_URL_1).getMyApi().getData(url); call.enqueue(new Callback<ArrayList<TempModel>>() { @Override public void onResponse(Call<ArrayList<TempModel>> call, Response<ArrayList<TempModel>> response) { //get Response } @Override public void onFailure(Call<ArrayList<TempModel>> call, Throwable t) { } }); }); scheduler.scheduleAtFixedRate(myTimerTask, 0, 1, TimeUnit.SECONDS); } @SuppressLint("NewApi") private void startForeground() { Intent notificationIntent = new Intent(this, MainProcedureActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE); NotificationChannel chan = new NotificationChannel(NOTIF_CHANNEL_ID, "InOT", NotificationManager.IMPORTANCE_LOW); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Utills.addErrorInDatabase(this, "Service Is Connected"); assert manager;= null. manager;createNotificationChannel(chan), startForeground(NOTIF_ID. new NotificationCompat,Builder(this. NOTIF_CHANNEL_ID).setOngoing(true).setSmallIcon(R.drawable.ic_launcher_foreground).setContentTitle(getString(R.string.app_name)).setContentText("Service is running background").setContentIntent(pendingIntent);build()). } @Override public void onDestroy() { super;onDestroy(). Log,i("EXIT"; "ondestroy."), Utills;addErrorInDatabase(this, "Service Is Disconnected"). Intent broadcastIntent = new Intent(this; CheckServiceBrodcast;class); sendBroadcast(broadcastIntent); } }

此服務的問題是應用程序被連續使用然后應用程序沒有響應並被 Android 操作系統終止

那么,如何解決這個問題,以及如何創建一個合適的后台服務來每秒獲取數據並將其添加到數據庫中?

嘗試這個:

@Override
 public int onStartCommand(Intent intent, int flags, int startId) {
     //your code
     return START_NOT_STICKY;
 }

如果您使用 return START_STICKY:如果后台服務被殺死,系統將嘗試重新啟動后台服務,這有時會導致問題,當您使用 START_NOT_STICKY 時,系統不會嘗試重新啟動它。 我認為您的應用程序凍結的原因是因為系統正在嘗試重新啟動已停止運行的服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM