安卓服务在一段时间后停止

huangapple go评论98阅读模式
英文:

Android Service stops after some time

问题

我正在使用这段代码作为我项目的示例,以在锁定屏幕上显示一个片段,但仅在主活动未被销毁(未从最近任务中移除)时才起作用,有时仅在某段时间内起作用,然后停止在屏幕上显示片段。

如何使一个服务持续运行?新的API是否支持这样做?

英文:

I am using this code as a sample for my project to show a fragment on lock screen but it only works till the main activity is not destroyed (not removed from recents) and sometime only works for sometime and than it stops showing fragment on screen..

How can I run a service for all time ? also does new APIs supports it ..

答案1

得分: 1

在清单文件中初始化服务,然后在启动按钮中调用服务,如下所示:

  1. Intent i = new Intent(this, AnotherService.class);
  2. i.setAction("C.ACTION_START_SERVICE");
  3. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  4. startForegroundService(i);
  5. } else {
  6. startService(i);
  7. }

然后,您需要像下面这样启动服务:

  1. public class AnotherService extends Service {
  2. static final int NOTIFICATION_ID = 543;
  3. public AnotherService() {
  4. }
  5. @Override
  6. public IBinder onBind(Intent intent) {
  7. throw new UnsupportedOperationException("Not yet implemented");
  8. }
  9. @Override
  10. public void onCreate() {
  11. super.onCreate();
  12. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
  13. startMyOwnForeground();
  14. else
  15. startForeground(1, new Notification());
  16. // 在这里添加您的代码
  17. }
  18. @RequiresApi(Build.VERSION_CODES.O)
  19. private void startMyOwnForeground() {
  20. String NOTIFICATION_CHANNEL_ID = "example.permanence";
  21. String channelName = "Background Service";
  22. NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
  23. chan.setLightColor(Color.BLUE);
  24. chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
  25. NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  26. assert manager != null;
  27. manager.createNotificationChannel(chan);
  28. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
  29. Notification notification = notificationBuilder.setOngoing(true)
  30. .setContentTitle("App is running in background")
  31. .setPriority(NotificationManager.IMPORTANCE_MIN)
  32. .setCategory(Notification.CATEGORY_SERVICE)
  33. .build();
  34. startForeground(2, notification);
  35. }
  36. @Override
  37. public int onStartCommand(Intent intent, int flags, int startId) {
  38. return START_STICKY;
  39. }
  40. @Override
  41. public void onDestroy() {
  42. super.onDestroy();
  43. }
  44. }

如果您完成了服务的设置,然后在您想要停止的活动中,将停止按钮的代码更改为以下内容:

  1. stopService(new Intent(YourActivity.this, AnotherService.class));
英文:

initialize the service in the manifest and the you have to call the service as below in the start button

  1. Intent i = new Intent(this, AnotherService.class);
  2. i.setAction("C.ACTION_START_SERVICE");
  3. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  4. startForegroundService(i);
  5. }
  6. else
  7. {
  8. startService(i);
  9. }

then you have to start the service just like below

  1. public class AnotherService extends Service {
  2. static final int NOTIFICATION_ID = 543;
  3. public AnotherService() {
  4. }
  5. @Override
  6. public IBinder onBind(Intent intent) {
  7. // TODO: Return the communication channel to the service.
  8. throw new UnsupportedOperationException("Not yet implemented");
  9. }
  10. @Override
  11. public void onCreate() {
  12. super.onCreate();
  13. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
  14. startMyOwnForeground();
  15. else
  16. startForeground(1, new Notification());
  17. //your code here
  18. }
  19. @RequiresApi(Build.VERSION_CODES.O)
  20. private void startMyOwnForeground() {
  21. String NOTIFICATION_CHANNEL_ID = "example.permanence";
  22. String channelName = "Background Service";
  23. NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
  24. chan.setLightColor(Color.BLUE);
  25. chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
  26. NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  27. assert manager != null;
  28. manager.createNotificationChannel(chan);
  29. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
  30. Notification notification = notificationBuilder.setOngoing(true)
  31. .setContentTitle("App is running in background")
  32. .setPriority(NotificationManager.IMPORTANCE_MIN)
  33. .setCategory(Notification.CATEGORY_SERVICE)
  34. .build();
  35. startForeground(2, notification);
  36. }
  37. @Override
  38. public int onStartCommand(Intent intent, int flags, int startId) {
  39. return START_STICKY;
  40. //return super.onStartCommand(intent, flags, startId);
  41. }
  42. @Override
  43. public void onDestroy() {
  44. super.onDestroy();
  45. }

if you done the service then you have to change the stop button code with this in your activity where you want to stop

  1. stopService(new Intent(YourActivity.this, AnotherService.class));

huangapple
  • 本文由 发表于 2020年9月17日 01:15:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63924993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定