英文:
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
在清单文件中初始化服务,然后在启动按钮中调用服务,如下所示:
Intent i = new Intent(this, AnotherService.class);
i.setAction("C.ACTION_START_SERVICE");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(i);
} else {
startService(i);
}
然后,您需要像下面这样启动服务:
public class AnotherService extends Service {
static final int NOTIFICATION_ID = 543;
public AnotherService() {
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
// 在这里添加您的代码
}
@RequiresApi(Build.VERSION_CODES.O)
private void startMyOwnForeground() {
String NOTIFICATION_CHANNEL_ID = "example.permanence";
String channelName = "Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
如果您完成了服务的设置,然后在您想要停止的活动中,将停止按钮的代码更改为以下内容:
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
Intent i = new Intent(this, AnotherService.class);
i.setAction("C.ACTION_START_SERVICE");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(i);
}
else
{
startService(i);
}
then you have to start the service just like below
public class AnotherService extends Service {
static final int NOTIFICATION_ID = 543;
public AnotherService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
//your code here
}
@RequiresApi(Build.VERSION_CODES.O)
private void startMyOwnForeground() {
String NOTIFICATION_CHANNEL_ID = "example.permanence";
String channelName = "Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
//return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
if you done the service then you have to change the stop button code with this in your activity where you want to stop
stopService(new Intent(YourActivity.this, AnotherService.class));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论