我了解到Service
类在Android中在后台运行,并且我也了解如何start
和stop
服务。
我只是想更好地了解如何使用Service
。
所以说我有一个在后台运行的服务。如何简单地从Web浏览器应用程序获取网页的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());
}
}