在后台服务中启动服务

问题描述 投票:0回答:1

是否可以从后台服务启动服务?

这仅在应用程序打开时有效...

Intent service = new Intent(this, MyForegroundSerivce.class);
service.setAction(Constants.ACTION.START_ACTION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
   startForegroundService(service);
} else {
   startService(service);
}

我没有真正启动该服务,我只是在收到推送通知时,通过动作来调用它。

java android
1个回答
0
投票

请使用此服务:

开始服务

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     ContextCompat.startForegroundService(MainActivity.this, new Intent(MainActivity.this, ServiceBG.class));
 } else {
     startService(new Intent(MainActivity.this, ServiceBG.class));
 }

服务类

public class ServiceBG extends Service {

    private static final String NOTIFICATION_CHANNEL_ID = "my_notification";
    private static final long TIME_INTERVAL = 10000;

    private Handler handlerThred;
    private Context mContext;


    @Override
    public void onCreate() {
        super.onCreate();


        mContext = this;

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setOngoing(false)
                .setSmallIcon(R.drawable.ic_notification)
                .setColor(getResources().getColor(R.color.white))
                .setPriority(Notification.PRIORITY_MIN);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                    NOTIFICATION_CHANNEL_ID, NotificationManager.IMPORTANCE_LOW);
            notificationChannel.setDescription(NOTIFICATION_CHANNEL_ID);
            notificationChannel.setSound(null, null);
            notificationManager.createNotificationChannel(notificationChannel);
            startForeground(1, builder.build());
        }
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.w("Service", "BGS > Started");

        if (handlerThred == null) {
            handlerThred = new Handler();
            handlerThred.post(runnableThred);
            Log.w("Service ", "BGS > handlerThred Initialized");
        } else {
            Log.w("Service ", "BGS > handlerThred Already Initialized");
        }

        return START_STICKY;
    }

    private Runnable runnableThred = new Runnable() {

        @Override
        public void run() {

            Log.w("Service ", "BGS >> Running");

            if (handlerSendLocation != null && runnableThred != null)
                handlerSendLocation.postDelayed(runnableThred, TIME_INTERVAL);
        }
    };

    @Override
    public void onDestroy() {


        Log.w("Service", "BGS > Stopped");

        stopSelf();
        super.onDestroy();
    }

}

AndroidManifest.xml中

 <service
            android:name=".ServiceBG"
            android:enabled="true"
            android:exported="true" />
© www.soinside.com 2019 - 2024. All rights reserved.