我正在 android 中尝试 运行 前台服务,但每当我 运行 我的应用程序时,应用程序就会冻结并且没有显示任何通知

I'm trying to run foreground service in android but whenever i run my app, app got freezed and not showing any notification

我已经在MapsActivity的onCreate方法中创建了一个Notification通道,并调用了startService方法来启动服务。

应用程序保持冻结状态直到服务执行结束

在logcat中没有提到引起的错误。 我还提到在 ManifestFile

中使用 FOREGROUND_SERVICE

主要Class

public class MapsActivity extends AppCompatActivity {
    public static final String CHANNEL_ID = "FenceCalculateChannel";
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
createNotificationChannel();
        Intent serviceIntent = new Intent(this,BackGroundDistanceCalculate.class);
        startService(serviceIntent);
}
public void createNotificationChannel() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID,
                    "Fence Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT);

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

背景距离计算服务Class

public class BackGroundDistanceCalculate extends Service {

    final public static String TAG = "BackGroundDistance";
    @Override
    public void onCreate() {
        super.onCreate();
    }

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

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

        int i;

        for(i=0;i<1000;i++)
        {
            Log.d(TAG,"Value of i is : "+ i);
            SystemClock.sleep(1000);
        }

        Notification notification =
                new NotificationCompat.Builder(this, CHANNEL_ID)
                        .setContentTitle("Calculating Distance")
                        .setContentText("Distance Calculation is running")
                        .setSmallIcon(R.drawable.ic_favorite)
                        .setContentIntent(pendingIntent)
                        .build();

        //startForeground(1, notification);
        return START_NOT_STICKY;
    }

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


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

此代码应在应用程序启动时创建一个 Notificationchannel,但无法正常工作。 我可以做哪些更改来制作此应用程序 运行?

根据官方文档,服务 运行s 在主线程(UI 线程)上运行,因此它阻塞了 UI,这就是您的应用冻结的原因。

您应该使用已经有工作线程的 IntentService 来 运行 您的工作。

或者您可以在服务中手动创建一个 AsyncTask 或线程来处理您的工作。

我看到有两件事你可以解决以使其正常工作。

  1. 您正在让 BackGroundDistanceCalculate 进入休眠状态 1000 秒,这使得主线程挂起,因为 Service class 的实例在主线程上运行。您可以启动一个新线程或从 IntentService 扩展(如果不使用 WorkManager 可能无法在 Pie+ 中工作)
  2. 您可以尝试使用 ContextCompat.startForegroundService() 来启动您的服务,并且在您的服务中,您必须先 post 通知,然后才能开始您的工作。