打开具有map的Activity时,服务必须是显式的:Android lollipop(5.0)

时间:2015-08-05 04:14:50

标签: android google-maps android-intent explicit-intent

我在FragmentActitvity中显示的谷歌地图实现了 要打开此活动,我会通过点击按钮开始另一项活动的意图(让他们说活动A)。

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MapActivity.class);
                startActivity(intent);

        }
    });

我的MapActivity是这样的:

public class MapActivity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener {
    private GoogleMap googleMap;
    private LocationClient locationClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    if (googleMap == null) {
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        googleMap.setMyLocationEnabled(true);

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
        }
    }

    locationClient = new LocationClient(this, this, this);
    locationClient.connect();
    mLocationRequest = LocationRequest.create();

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(5 * 1000);
    mLocationRequest.setSmallestDisplacement(0.1f);
    mLocationRequest.setFastestInterval(5 * 1000);
}
}

MapActivity还有其他接口实现方法。 但是当我点击按钮打开MapActivity时,我收到错误:

java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.google.android.location.internal.GoogleLocationManagerService.START }

我在谷歌搜索,我发现我需要创建一个明确的意图。但我不知道如何在我的情况下这样做。

1 个答案:

答案 0 :(得分:0)

尝试使用Android SDK Manager升级Android SDK Build-tools。选择构建工具的最新版本。然后在你的build.gradle中

compileSdkVersion 22
buildToolsVersion "22.0.1"

最好的方法是升级新的GoogleApiClient,你的新MapActivity应该是这样的

public class MapActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
    private GoogleMap googleMap;

    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            googleMap.setMyLocationEnabled(true);

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
            }
        }

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

    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mGoogleApiClient.disconnect();
    }

    @Override
    public void onConnected(Bundle bundle) {
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(5 * 1000);
        locationRequest.setSmallestDisplacement(0.1f);
        locationRequest.setFastestInterval(5 * 1000);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public void onLocationChanged(Location location) {
        // handle new location
    }
}

你应该使用最后的游戏服务。在你的build.gradle中

dependencies {

    compile 'com.google.android.gms:play-services-location:7.5.0'
    compile 'com.google.android.gms:play-services-maps:7.5.0'
}