前台服务Foreground Service

1.什么是前台服务

 前台服务是那些被认为用户知道(用户所认可的)且在系统内存不足的时候不容许系统杀死的服务。前台服务必须给状态栏提供一个通知,它被放到正在运行(Ongoing)标题之下——这就意味着通知只有这个服务被终止或从前台主动移除通知后才能被解除。

2.为什么使用

在一般情况下,Service几乎都是在后台运行,一直默默地做着辛苦的工作。但这种情况下,后台运行的Service系统优先级相对较低,当系统内存不足时,在后台运行的Service就有可能被回收。

那么,如果我们希望Service可以一直保持运行状态且不会在内存不足的情况下被回收时,可以选择将需要保持运行的Service设置为前台服务。

3.如何实现

<Button
            android:onClick="Startfor"
            android:text="开启前台服务"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></Button>
public void Startfor(View view) {
        Intent intent = new Intent(this,MyService.class);
        startService(intent);
    }
@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.item);
        remoteViews.setTextViewText(R.id.tv1,"落霞与孤鹜齐飞");
        remoteViews.setTextViewText(R.id.tv2,"秋水共长天一色");
        remoteViews.setImageViewResource(R.id.iv1,R.drawable.ic_launcher_background);
        Intent intent1 = new Intent(this,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,101,intent1,PendingIntent.FLAG_CANCEL_CURRENT);
        Notification.Builder builder=new  Notification.Builder(MyService.this);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setCustomContentView(remoteViews);
        Notification notification = builder.build();
        startForeground(1,notification);
        return super.onStartCommand(intent, flags, startId);
    }

关闭前天服务

<Button
            android:onClick="Stopfor"
            android:text="关闭前台服务"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></Button>
public void Stopfor(View view) {
        Intent intent = new Intent(this,MyService.class);
        stopService(intent);//停止服务
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要搭建前台服务,需要创建一个服务类并在其中实现服务的逻辑,然后将该服务绑定到应用程序的UI界面上。 下面是一个简单的前台服务搭建的步骤: 1. 创建一个服务类,继承自 Service 类。 ``` public class MyForegroundService extends Service { ... } ``` 2. 在服务类中实现服务的逻辑。 ``` public class MyForegroundService extends Service { ... @Override public int onStartCommand(Intent intent, int flags, int startId) { // 在此处编写服务逻辑 ... return START_STICKY; } ... } ``` 3. 在服务类中创建一个通知,用于在系统通知栏中显示服务的状态。 ``` public class MyForegroundService extends Service { ... @Override public void onCreate() { super.onCreate(); // 创建通知 Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("My Foreground Service") .setContentText("Service is running...") .setSmallIcon(R.drawable.ic_notification) .build(); // 启动服务,并将服务设置为前台服务 startForeground(1, notification); } ... } ``` 4. 在服务类中实现 onBind() 方法,返回 null。 ``` public class MyForegroundService extends Service { ... @Nullable @Override public IBinder onBind(Intent intent) { return null; } ... } ``` 5. 在应用程序的 UI 界面中绑定服务,并启动服务。 ``` Intent intent = new Intent(this, MyForegroundService.class); startService(intent); ``` 以上就是搭建前台服务的基本步骤。需要注意的是,在 Android 8.0 及以上版本中,前台服务必须创建一个 NotificationChannel 并将其与通知相关联,否则服务将无法启动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值