运行应用程序时服务无法启动 - 如何显式启动服务?

时间:2016-11-21 17:00:26

标签: android android-service

我已在我的Android应用中声明了两项服务,但在运行我的应用时,它们都没有开始。它不显示任何类型的错误。

public class SecondController {
    @FXML
    private TextField testArea;

    private FirstController firstController;

    private int localX;

    //constructor
    public SecondController() {
        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("First.fxml"));
        try
        {
            loader.load();
            firstController = loader.getController();

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }


    public void startSecondController(Stage stage)
    {
        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("Second.fxml"));
        try
        {
            Pane pane = loader.load();
            Scene scene = new Scene(pane, 300, 300);
            stage.setTitle("Second Window");
            stage.setScene(scene);
            stage.show();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @FXML
    void click(ActionEvent event) {
        localX = Integer.parseInt(testArea.getText());
        firstController.setX(localX);
    }

    @FXML
    public void initialize()
    {
        localX = firstController.getX();
        testArea.setText(Integer.toString(localX));
    }
}

1 个答案:

答案 0 :(得分:0)

你试过这些:

1)在您的服务代码中包含android:enabled="true"

  <service android:enabled="true" android:name=".MyFirebaseMessagingService" />

2)使用onStartCommand()方法使用Service方法开始startService()。点击相关信息的超链接。

PS:您的应用中似乎有许多活动,并希望在MainActivity本身启动服务。同时包括这些服务的Java类代码。它将帮助用户更好地帮助您。