安卓前台服务的使用(简单)

首先是 AndroidManifest.xml 文件


<!-- 使用前台服务的权限 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

创建一个service

 

然后输入该服务的类名就ok了

点击finish下一步

这个时候就创建好了

好了之后应该是这样的

public class MyService2 extends Service {
    public MyService2() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

在这个服务中重写onCreate方法

public class MyService2 extends Service {
    public MyService2() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     *     这里重写 onCreate
     */
    @Override
    public void onCreate() {
        super.onCreate();

//        判断是否为8.0版本以上
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//            获取系统服务管理器
            NotificationManager manage = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            String id = "通知id";
            String name = "通知分类名称";
//            建立通知通道
            NotificationChannel notificationChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT);
            manage.createNotificationChannel(notificationChannel);

            Notification build = new NotificationCompat.Builder(this, id)
                    .setContentTitle("前台服务")
                    .setContentText("这是一个前台服务")
                    .setWhen(System.currentTimeMillis())   //   当前时间
                    .setSmallIcon(R.drawable.ic_launcher_background)    //  图标
                    .setProgress(100, 10, false)   //   进度
                    .build();

            //  第一个参数唯一就好
            startForeground(1, build);    
            manage.notify(1, build);

        }
    }
    /**
     *  =========================================================
     */
}

最后就是在MainActivity中绑定一个点击事件即可

startService(new Intent(this, MyService2.class)  //  开启服务

 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
当我们需要在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); ``` 这样,当启动前台服务时,会在通知栏中显示一个持续运行的通知,告知用户应用正在后台执行任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值