Android Service服务(有例子)

服务

1.服务简介
服务(Service)是Android中的四大组件之一,是一个长期运行在后台的用户组件,没有用户界面。即使切换到另一个应用程序,服务也可以在后台运行,因此服务更适合执行一段时间而又不需要显示界面的后台操作,例如下载数据,播放音乐等。

2.服务的创建
在程序包上右键,选择【new】>>【Service】>>【Service】选项,在弹出窗口上输入名称即可。
在这里插入图片描述
那两个一般都选上。

3.具体例子

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_inputPath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Music/a.mp3"
        android:textColor="#09C9EB"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_play"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/play"
            android:drawablePadding="3dp"
            android:gravity="center"
            android:textSize="30sp"
            android:text
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
当我们需要在Android应用中执行一些长时间运行的任务时,可以使用前台服务。前台服务是一种在通知栏中显示持续运行通知的服务,以提醒用户应用正在后台执行任务。 以下是一个简单的Android前台服务例子: 1. 创建一个Service类,继承自android.app.Service,并重写onCreate()、onStartCommand()和onDestroy()方法。 ```java public class MyForegroundService extends Service { private static final int NOTIFICATION_ID = 1; private static final String CHANNEL_ID = "ForegroundServiceChannel"; @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { createNotificationChannel(); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("Foreground Service") .setContentText("Running...") .setSmallIcon(R.drawable.ic_notification) .setContentIntent(pendingIntent) .build(); startForeground(NOTIFICATION_ID, notification); // 执行长时间运行的任务 return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); stopForeground(true); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "Foreground Service Channel", NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } } } ``` 2. 在AndroidManifest.xml文件中注册前台服务。 ```xml <service android:name=".MyForegroundService" android:enabled="true" android:exported="false" /> ``` 3. 在需要启动前台服务的地方,使用以下代码启动服务。 ```java Intent serviceIntent = new Intent(this, MyForegroundService.class); ContextCompat.startForegroundService(this, serviceIntent); ``` 这样,当启动前台服务时,会在通知栏中显示一个持续运行的通知,告知用户应用正在后台执行任务。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值