【问题标题】:Service must be explicit when opening Activity having map: Android lollipop(5.0)打开具有地图的 Activity 时服务必须是显式的:Android lollipop(5.0)
【发布时间】:2015-08-05 04:14:50
【问题描述】:

我在 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 }

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

【问题讨论】:

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


    【解决方案1】:

    尝试使用 Android SDK Manager 升级您的 Android SDK 构建工具。选择构建工具的最新版本。然后在你的 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'
    }
    

    【讨论】:

    • 降级并不是一个真正的选择。
    • @NikolaDespotoski 我同意。已编辑
    • 我引用了这个:-stackoverflow.com/questions/26530565/… 链接,我发现在调用 bindService() 之前我需要 setPackage(),但我不知道在我的代码中需要在哪里进行此更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2015-05-13
    相关资源
    最近更新 更多