Я понимаю, что класс Service
работает в фоновом режиме в Android, и я также понимаю, как start
и stop
служба.
Я просто пытаюсь получить лучшее представление о том, как использовать Service
.
Итак, скажем, у меня служба работает в фоновом режиме.Как я могу просто получить URL-адрес веб-страницы из приложения веб-браузера.
Ее моя служба ниже:
public class MyService 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) {
// do your jobs here
startForeground();
return super.onStartCommand(intent, flags, startId);
}
private void startForeground() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
startForeground(NOTIF_ID, new NotificationCompat.Builder(this,
NOTIF_CHANNEL_ID) // Create a Notification Channel
.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(getString(R.string.runningtitle))
.setContentText(getString(R.string.runningtext))
.setContentIntent(pendingIntent)
.build());
}
}