为什么我不能从我的活动中调用此服务?

伊万

我尝试用两种不同的方式调用此服务,但它似乎没有用。

第一种方法是:

startService(new Intent(getBaseContext(), LocationService.class));

为此,我收到一条错误消息,说“

由以下原因引起:`java.lang.IllegalStateException:GoogleApiClient尚未连接。

然后我尝试了这个:

Intent serviceIntent = new Intent();
                serviceIntent.setAction("com.parseapp.eseen.eseen.service.LocationService");
                startService(serviceIntent);

而且它没有用,相反,绝对没有任何反应,logcat中没有任何显示。有人可以帮忙吗?这是整个代码:

LocationService.class

public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, LocationListener {

// LogCat tag
private static final String TAG = LocationService.class.getSimpleName();

private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;

private Location mLastLocation;

// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;

// boolean flag to toggle periodic location updates
private boolean mRequestingLocationUpdates = false;

private LocationRequest mLocationRequest;

// Location updates intervals in sec
private static int UPDATE_INTERVAL = 10000; // 10 sec
private static int FATEST_INTERVAL = 5000; // 5 sec
private static int DISPLACEMENT = 10; // 10 meters


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

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API).build();


}

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

    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
        togglePeriodicLocationUpdates();
    }



    return START_NOT_STICKY;
}

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

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FATEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}

private void togglePeriodicLocationUpdates() {
    mGoogleApiClient.connect();

    if (!mRequestingLocationUpdates) {

        mRequestingLocationUpdates = true;

        startLocationUpdates();

        Log.d(TAG, "Periodic location updates started!");

    } else {

        mRequestingLocationUpdates = false;

        // Stopping the location updates
        stopLocationUpdates();

        Log.d(TAG, "Periodic location updates stopped!");
    }
}

protected void stopLocationUpdates() {
    LocationServices.FusedLocationApi.removeLocationUpdates(
            mGoogleApiClient, this);
}

protected void startLocationUpdates() {
    mGoogleApiClient.connect();
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);

}

@Override
public void onConnected(Bundle arg0) {
    createLocationRequest();
}

@Override
public void onConnectionSuspended(int arg0) {
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
            + result.getErrorCode());
}

@Override
public void onLocationChanged(Location location) {
    // Assign the new location
    mLastLocation = location;

    Toast.makeText(getApplicationContext(), "Location changed!",
            Toast.LENGTH_SHORT).show();
}

@Override
public boolean stopService(Intent name) {
    return super.stopService(name);
}

SearchActivity.class

button = (Button)findViewById(R.id.buttonPressed);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent serviceIntent = new Intent();
            serviceIntent.setAction("com.parseapp.eseen.eseen.service.LocationService");
            startService(serviceIntent);
        }
    });

AndroidManifest.xml

<service android:name=".LocationService">

        <intent-filter>
            <action android:name=".LocationService"> </action>
        </intent-filter>

    </service>

   
鲍勃·斯奈德

使用类名启动服务的首次尝试有效:

startService(new Intent(getBaseContext(), LocationService.class));

服务开始执行后发生了异常。在成功完成GoogleApi连接之前,即在调用onConnected()之后,您的代码才能使用LocationServices。您的代码在启动startLocationUpdates()之前会多次调用connect(),但不会等待onConnected()被调用。该方法是建立连接并可以使用LocationServices时收到的通知。

演示代码用于AsyncTask,而不用于Service,但使您了解如何使用CountDownLatch完成连接处理。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我不能从Google AppScript for Sheets的侧栏中调用服务器功能?

来自分类Dev

为什么我不能从Angular调用ajax调用

来自分类Dev

为什么我不能从我用泛型创建的类中调用方法?爪哇

来自分类Dev

为什么我不能从我用泛型创建的类中调用方法?爪哇

来自分类Dev

Swift:为什么我不能从覆盖init调用方法?

来自分类Dev

为什么我不能从嵌入式UITableViewController调用存储在父UIViewController中的方法?

来自分类Dev

为什么我不能从 Ionic 4 中的 API 服务返回数据?

来自分类Dev

为什么我不能从远程分支中拉出?

来自分类Dev

为什么我不能从异步代码中捕获异常?

来自分类Dev

我为什么不能从情节提要中创建IBAction

来自分类Dev

为什么我不能从列表中删除所需的元素

来自分类Dev

为什么我不能从该表中删除记录?

来自分类Dev

为什么我不能从XPath查询中检索URL?

来自分类Dev

为什么我不能从C中的函数传回链表?

来自分类Dev

为什么我不能从远程分支中拉出?

来自分类Dev

为什么我不能从开关内的变量中减去?

来自分类Dev

为什么我不能从网络外部访问我的Web服务器?

来自分类Dev

为什么我不能取消绑定此服务?

来自分类Dev

为什么我不能从Main类显示此自定义JFrame?

来自分类Dev

为什么我们不能从派生类中调用受保护的析构函数?

来自分类Dev

当对象是子类类型时,为什么我不能从超类调用子类中的公共方法?

来自分类Dev

通过ajax调用,为什么我的PHP文档中不能使用此$ var post数据?

来自分类Dev

Perl:为什么我可以递归地调用子例程A,但是我不能从B调用A,而B是从较早的A调用的呢?

来自分类Dev

为什么我不能从我的SSD引导?

来自分类Dev

为什么我不能从事件侦听器调用方法,而不能在类中的其他地方调用方法?

来自分类Dev

为什么我不能从另一个类调用方法

来自分类Dev

为什么我不能从远程访问svn服务器?

来自分类Dev

为什么我不能从我的Json文件中检索数据?

来自分类Dev

为什么我不能从 sqlite 数据库中检索到我期望的数据?

Related 相关文章

  1. 1

    为什么我不能从Google AppScript for Sheets的侧栏中调用服务器功能?

  2. 2

    为什么我不能从Angular调用ajax调用

  3. 3

    为什么我不能从我用泛型创建的类中调用方法?爪哇

  4. 4

    为什么我不能从我用泛型创建的类中调用方法?爪哇

  5. 5

    Swift:为什么我不能从覆盖init调用方法?

  6. 6

    为什么我不能从嵌入式UITableViewController调用存储在父UIViewController中的方法?

  7. 7

    为什么我不能从 Ionic 4 中的 API 服务返回数据?

  8. 8

    为什么我不能从远程分支中拉出?

  9. 9

    为什么我不能从异步代码中捕获异常?

  10. 10

    我为什么不能从情节提要中创建IBAction

  11. 11

    为什么我不能从列表中删除所需的元素

  12. 12

    为什么我不能从该表中删除记录?

  13. 13

    为什么我不能从XPath查询中检索URL?

  14. 14

    为什么我不能从C中的函数传回链表?

  15. 15

    为什么我不能从远程分支中拉出?

  16. 16

    为什么我不能从开关内的变量中减去?

  17. 17

    为什么我不能从网络外部访问我的Web服务器?

  18. 18

    为什么我不能取消绑定此服务?

  19. 19

    为什么我不能从Main类显示此自定义JFrame?

  20. 20

    为什么我们不能从派生类中调用受保护的析构函数?

  21. 21

    当对象是子类类型时,为什么我不能从超类调用子类中的公共方法?

  22. 22

    通过ajax调用,为什么我的PHP文档中不能使用此$ var post数据?

  23. 23

    Perl:为什么我可以递归地调用子例程A,但是我不能从B调用A,而B是从较早的A调用的呢?

  24. 24

    为什么我不能从我的SSD引导?

  25. 25

    为什么我不能从事件侦听器调用方法,而不能在类中的其他地方调用方法?

  26. 26

    为什么我不能从另一个类调用方法

  27. 27

    为什么我不能从远程访问svn服务器?

  28. 28

    为什么我不能从我的Json文件中检索数据?

  29. 29

    为什么我不能从 sqlite 数据库中检索到我期望的数据?

热门标签

归档