无法在Api> 26中启动后台服务

时间:2019-10-12 18:08:10

标签: java android android-service android-9.0-pie

尝试从后台服务启动服务,以某种方式使其可用于低于api 24的所有android版本,但实际上无法在(oreo,Pie,..)中使用。

但是我在下面尝试此代码,如果使用+ api> 24 ....

进行测试,则会遇到此问题屏幕。

mklement0

很高兴为您提供帮助,谢谢!

1)主要活动(启动服务):

 public void startService(View v) {
    String input = editTextInput.getText().toString();

    Intent serviceIntent = new Intent(this, ExampleService.class);
    serviceIntent.putExtra("inputExtra", input);

    ContextCompat.startForegroundService(this, serviceIntent);

     Toast.makeText(Options.this,"Notification, On.", Toast.LENGTH_LONG).show();
}

public void stopService(View v) {
    Intent serviceIntent = new Intent(this, ExampleService.class);
    stopService(serviceIntent);

    Toast.makeText(Options.this,"Notification, Off.", Toast.LENGTH_LONG).show();
}

2)后台服务类:

package com.demo.testttt;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import static com.demo.testttt.App.CHANNEL_ID;


public class ExampleService extends Service {


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

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



    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        String input = intent.getStringExtra("inputExtra");

        Intent notificationIntent = new Intent(this, Home.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Example Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.logo)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);

        //do heavy work on a background thread
        //stopSelf();

    }
    else {

        String input = intent.getStringExtra("inputExtra");

        Intent notificationIntent = new Intent(this, Home.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        Notification notification = new Notification.Builder(ExampleService.this)
                .setVibrate(new long[] { 350, 350})
                .setContentTitle("Example Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.logo)
                .setContentIntent(pendingIntent)
                .build();

        NotificationManager notifmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      ///  notifmanager.notify(0,notification);
        startForeground(1, notification);

    }
    return START_NOT_STICKY;
}


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

}



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

3)应用类别:

package package com.demo.testttt;

import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;


public class App extends Application {
public static final String CHANNEL_ID = "exampleServiceChannel";

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

    createNotificationChannel();
}

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel serviceChannel = new NotificationChannel(
                CHANNEL_ID,
                "Example Service Channel",
                NotificationManager.IMPORTANCE_DEFAULT
        );

        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(serviceChannel);
    }
}
}

4)清单xml:

    <service
        android:name=".ExampleService"
        android:permission="android.permission.BIND_JOB_SERVICE"
        android:enabled="true"
        android:exported="true"/>

1 个答案:

答案 0 :(得分:0)

您在清单中的权限在哪里。

您应该获得FOREGROUND_SERVICE的许可。

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />