小编Jas*_*Law的帖子

FusedLocationApi 后台位置服务停止随机接收更新

我正在尝试将 FusedLocationApi 与待处理的意图一起使用来获取期间位置更新,以便我可以将数据发送到某个服务器进行处理。一切正常。但是,在某些情况下,广播会停止接收。我需要再次重新启动该服务才能继续。

我已经有了 onStartCommand 服务来返回 START_STICKY,所以即使应用程序被终止,它也应该再次启动该服务。此外,我还添加了一个 Boot Completed Intent Receiver,因此如果手机死机并且用户重新启动手机,它将重新启动我的服务。

所以看起来一切都很好,但只是在某个时候,一切都停止了。我确实注意到几次,当它停止工作时,我收到的最后一个位置是 NULL(我记录了整个项目中的每个位置更新和错误消息)。

任何想法为什么定位服务停止工作?

PS 没有连接失败,因为我在该函数中放入了一条消息并挂钩,但它没有被调用。这也不是互联网故障,因为我也记录了它。一旦互联网恢复,它将继续正常/预期。

谢谢。

这是主要活动:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getSupportActionBar();

    if (actionBar != null) {
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        if (findViewById(android.R.id.home) != null) {
            findViewById(android.R.id.home).setVisibility(View.GONE);
        }

        LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflator.inflate(R.layout.header_logo, null);

        ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(view, params);
    }

    setContentView(R.layout.activity_main);

    TextView textView = (TextView) findViewById(R.id.status_text);

    // init preferences and …
Run Code Online (Sandbox Code Playgroud)

android background-service location-services fusedlocationproviderapi android-fusedlocation

6
推荐指数
0
解决办法
1001
查看次数