先建立一个文件ForegroundService.class,继承Service类

public class ForegroundService extends Service {
    ..
}

算了懒得写了,直接Copy改改就能用了

public class ForegroundService extends Service {

    public static final int NOTICE_ID = 18031;
    public static final String CHANNEL_ID = "18031";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
        Notification notification;
        NotificationManager manager = (NotificationManager) getApplication().getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Android8.0 及以上
            Uri mUri = Settings.System.DEFAULT_NOTIFICATION_URI;
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, "driver", NotificationManager.IMPORTANCE_LOW);
            mChannel.setDescription("description");
            mChannel.setSound(mUri, Notification.AUDIO_ATTRIBUTES_DEFAULT);
            manager.createNotificationChannel(mChannel);
            notification = new Notification.Builder(this, CHANNEL_ID)
                    .setChannelId(CHANNEL_ID)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(getString(R.string.app_name))
                    .setContentIntent(pi)
                    .build();
        } else {
            notification = new Notification.Builder(this)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(getString(R.string.app_name))
                    .setContentIntent(pi)
                    .build();
        }
        // 提升应用权限
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        notification.flags |= Notification.FLAG_NO_CLEAR;
        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
        startForeground(NOTICE_ID, notification);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 当资源允许情况下,重启service
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        stopForeground(true);
        super.onDestroy();
    }

}

记得在manifest里注册

<manifest>
    ..
    <!-- Android 9.0及以上需要权限 -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    ..
    <application>
        ..
        <service android:name=".ForegroundService" />
        ..
    </application>
</manifest>

在MainActivity中启动

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ..
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(new Intent(mContext, ForegroundService.class));
    } else {
        startService(new Intent(mContext, ForegroundService.class));
    }
    ..
}

停止

@Override
protected void onDestroy() {
    ..
    stopService(new Intent(mContext, ForegroundService.class));
    ..
    super.onDestroy();
}

标签: 安卓, 前台服务

添加新评论