从运行服务获取网站数据

时间:2019-02-24 16:46:36

标签: java android android-service

我了解到Service类在Android中在后台运行,并且我也了解如何startstop服务。

我只是想更好地了解如何使用Service

所以说我有一个后台运行的服务,当我使用Chrome,Firefox,Samsung Internet等导航到随机网站时,如何在服务中打印网页的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());
  }
}

0 个答案:

没有答案