Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java flatter/Android找不到符号startForeground()_Java_Android_Flutter - Fatal编程技术网

Java flatter/Android找不到符号startForeground()

Java flatter/Android找不到符号startForeground(),java,android,flutter,Java,Android,Flutter,我一直在尝试让一个后台服务在Android上运行。 但是Android一直告诉我,尽管我正确处理了导入,但还是找不到startForeground() package com.example.helloservice; import android.app.Notification; import android.content.ContextWrapper; import android.content.Intent; import android.content.IntentFilter;

我一直在尝试让一个后台服务在Android上运行。 但是Android一直告诉我,尽管我正确处理了导入,但还是找不到
startForeground()

package com.example.helloservice;

import android.app.Notification;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.app.PendingIntent;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Activity;
import android.view.View;
import android.os.IBinder;
import android.util.Log;
import android.app.Service;


import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
  private static final String CHANNEL = "samples.flutter.io/battery";

  Intent i = new Intent(this, HelloService.class);
  Intent notificationIntent = new Intent(this, MainActivity.class);
  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

  Notification notification = new Notification.Builder(this).setContentTitle("My Awesome")
      .setContentText("Doing some work").setContentIntent(pendingIntent).build();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
    // createNotificationChannel();
    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(new MethodCallHandler() {
      @Override
      public void onMethodCall(MethodCall call, Result result) {
        if (call.method.equals("getBatteryLevel")) {
          int batteryLevel = getBatteryLevel();
          if (batteryLevel != -1) {
            result.success(batteryLevel);

            startService(i);
            startForeground(1337, notification);

..

  }
}
如果有帮助的话,我已经将gradlecompileversion设置为>27,并且我的清单已经用服务标签设置好了


这是通过平台代码使用flatter实现的,我尝试启动了一个新项目,但没有解决问题。

调用
startForeground()
是在
服务中进行的,而不是在
活动中进行的。
startForegroundService()
方法是您希望在
活动中使用的方法,以代替
startService()
。谢谢,我不确定我怎么会错过它!我认为整个通知也应该在服务中构建?是的,你可以这样做。我通常将它粘贴在实用程序类中的静态方法中,因此如果需要,我可以从其他地方更新
通知。如果您不需要这样做,那么可以在
服务中完成这一切。