【问题标题】:Process: Unable to start service com.google.android.gms.checkin.CheckinService with Intent进程:无法使用 Intent 启动服务 com.google.android.gms.checkin.CheckinService
【发布时间】:2014-06-09 03:09:53
【问题描述】:

我正在尝试构建一个 Google 地图应用程序,但在我的 LogCat 中不断收到它。我在清单中拥有所有权限和元数据集,但仍然对这个错误感到目瞪口呆。已在 SO 上到处查看此特定错误,但没有发现与 com.google.android.gms.checkin

相关的任何内容

关于我的结构层次结构。 MainActivity 扩展了 ActionBarActivity,在操作栏下方添加了三个选项卡。每个选项卡都有自己的片段。在gMapFragment 上,我从我的GPSTrack 类创建了一个GPSTrack 对象,该类扩展了Service 并实现了LocationListener

问题是,当我启动应用程序时,我收到以下消息:

我已正确导入所有库,我什至将 google-play-services.jar 添加到我的 libs 文件夹中。我还通过 CMD 将 Google Play Services APK 安装到了我的模拟器上。

此外,我的GPSTrack 类中的LocationManager lm = = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); 总是返回null

为什么会这样,我该如何解决这些问题?希望能得到解释以及解决方案,我想了解这里发生了什么。

==============

代码:

gMapFragment.java

public class gMapFragment extends SupportMapFragment {

private final String TAG = "gMapFragment";

private GoogleMap mMap;
protected SupportMapFragment mapFrag;

private Context mContext = getActivity();

private static View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
    }
    try {
        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.fragment_map, container, false);
        setupGoogleMap();
    } catch (Exception e) {
        /*
         * Map already there , just return as view
         */
    }
    return view;
}

private void setupGoogleMap() {
    mapFrag = (SupportMapFragment) getFragmentManager().findFragmentById(
            R.id.mapView);
    if (mapFrag == null) {
        FragmentManager fragManager = getFragmentManager();
        FragmentTransaction fragTransaction = fragManager
                .beginTransaction();
        mapFrag = SupportMapFragment.newInstance();
        fragTransaction.replace(R.id.mapView, mapFrag).commit();
    }
    if (mapFrag != null) {
        mMap = mapFrag.getMap();
        if (mMap != null) {
            setupMap();
            mMap.setOnMapClickListener(new OnMapClickListener() {

                @Override
                public void onMapClick(LatLng point) {
                    // TODO your click stuff on map

                }

            });

        }
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.d("Attach", "on attach");
}

@Override
public void onDetach() {
    super.onDetach();
}

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

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
}

private void setupMap() {

    GPSTrack gps = new GPSTrack(mContext);

    // Enable MyLocation layer of google map
    mMap.setMyLocationEnabled(true);
    Log.d(TAG, "MyLocation enabled");
    // Set Map type
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    // Grab current location
    **ERROR HERE/Returns Null** Location location = gps.getLocation();
    Log.d(TAG, "Grabbing location...");

    if (location != null) {
        Log.d(TAG, "location != null");
        // Grab Latitude and Longitude
         double latitude = location.getLatitude();
         double longitude = location.getLongitude();
         Log.d(TAG, "Getting lat, long..");
        // Initialize LatLng object
        LatLng latLng = new LatLng(latitude, longitude);
        Log.d(TAG, "LatLng initialized");
        // Show current location on google map
         mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        // Zoom in on google map
        mMap.animateCamera(CameraUpdateFactory.zoomTo(20));
        mMap.addMarker(new MarkerOptions().position(
                new LatLng(latitude, longitude)).title("You are here."));

    } else {
        gps.showSettingsAlert();
    }
}
}

GPSTrack.java

public class GPSTrack extends Service implements LocationListener{

private final Context mContext;

private boolean isGPSEnabled = false;

//See if network is connected to internet
private boolean isNetworkEnabled = false;

//See if you can grab the location
private boolean canGetLocation = false;

protected Location location = null;
protected double latitude;
protected double longitude;

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 10; //10 Meters

private static final long MINIMUM_TIME_CHANGE_FOR_UPDATES = 1000 * 60 * 1; //1 minute

protected LocationManager locationManager;


public GPSTrack(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        //Setup locationManager for controlling location services
        **ERROR HERE/Return Null** locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

        //See if GPS is enabled
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        //See if Network is connected to the internet or carrier service
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            Toast.makeText(getApplicationContext(), "No Network Provider Available", Toast.LENGTH_SHORT).show();
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, 
                        MINIMUM_TIME_CHANGE_FOR_UPDATES, 
                        MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
                        this);
                Log.d("GPS", "GPS Enabled");

                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }

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

    return location;
}

public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTrack.this);
    }
}

public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }

    return latitude;
}

public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }

    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}
    public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    //AlertDialog title
    alertDialog.setTitle("GPS Settings");

    //AlertDialog message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to Settings?");

    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(i);
            }
        });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }
        });
    alertDialog.show();

}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

logcat

06-08 22:35:03.441: E/AndroidRuntime(1370): FATAL EXCEPTION: main
06-08 22:35:03.441: E/AndroidRuntime(1370): Process: com.google.android.gms, PID: 1370
06-08 22:35:03.441: E/AndroidRuntime(1370): java.lang.RuntimeException: Unable to start service com.google.android.gms.checkin.CheckinService@b1094e48 with Intent { cmp=com.google.android.gms/.checkin.CheckinService }: java.lang.SecurityException: attempting to read gservices without permission: Neither user 10053 nor current process has com.google.android.providers.gsf.permission.READ_GSERVICES.
06-08 22:35:03.441: E/AndroidRuntime(1370):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2719)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at android.app.ActivityThread.access$2100(ActivityThread.java:135)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at android.os.Looper.loop(Looper.java:136)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at java.lang.reflect.Method.invokeNative(Native Method)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at java.lang.reflect.Method.invoke(Method.java:515)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at dalvik.system.NativeStart.main(Native Method)
06-08 22:35:03.441: E/AndroidRuntime(1370): Caused by: java.lang.SecurityException: attempting to read gservices without permission: Neither user 10053 nor current process has com.google.android.providers.gsf.permission.READ_GSERVICES.
06-08 22:35:03.441: E/AndroidRuntime(1370):     at android.app.ContextImpl.enforce(ContextImpl.java:1685)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1714)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at android.content.ContextWrapper.enforceCallingOrSelfPermission(ContextWrapper.java:572)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at imq.c(SourceFile:107)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at imq.a(SourceFile:121)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at imq.a(SourceFile:227)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at bwq.c(SourceFile:166)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at com.google.android.gms.checkin.CheckinService.a(SourceFile:237)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at com.google.android.gms.checkin.CheckinService.onStartCommand(SourceFile:211)
06-08 22:35:03.441: E/AndroidRuntime(1370):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2702)

AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<uses-permission android:name="com.app.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <service
        android:name="com.app.GPSTrack"
        android:exported="false"
        android:enabled="true"
        android:label="@string/service_name" >
    </service>
    <activity
        android:name="com.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AI........................" />
</application>

编辑:

&lt;application /&gt;里面添加了这个

<service
        android:name="com.app.GPSTrack"
        android:exported="false"
        android:enabled="true"
        android:label="@string/service_name" >
    </service>

但仍然在 logcat 中获取 location == null

【问题讨论】:

    标签: android google-maps google-play-services locationmanager runtimeexception


    【解决方案1】:

    您应该在应用程序清单文件中注册您的 Service 类,即 GPSTracker 类:

    <service
      android:name="MyService"
      android:icon="@drawable/icon"
      android:label="@string/service_name"
      android:exported="false"
      >
    </service> 
    

    添加&lt;permission&gt;标签:

     <permission
            android:name="com.app.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />
    

    P.S : 如果您需要参考,可以关注link

    【讨论】:

    • 谢谢,需要在android:name 中叫它“MyService”吗?想了解这一切。现在就试试:)
    • 试过但没有用,我在 中添加了它并且位置仍然 == null
    • 您在清单文件中为声明服务提供了什么名称?否则你可以在这里更新你的问题
    • 您的清单中遗漏了一项权限。我会更新我的答案
    • 给定的名字是 android:name="com.app.GPSTrack" 也设置了 android:enabled="true"
    【解决方案2】:

    我在实际设备上对其进行了测试,它能够获取我当前的位置。我想知道为什么模拟器不起作用!

    更新

    这对我有用:

    private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            //Do something when location changes
        }
    };
    

    然后在您的地图设置方法中,您可以调用:

    GoogleMap.setOnMyLocationChangeListener(myLocationChangeListener);
    

    停止Google Play Services Has Stopped 问题

    1. 我下载了 Genymotion 模拟器
    2. 已安装 ARM
    3. 已安装 Google Play 服务 APK
    4. 现在可以通过模拟 GPS 使用 Google 地图

    【讨论】:

    • 实际上从头开始,因为它仍然没有使用我在物理设备上的代码中的 GoogleMap 设置,这很奇怪。这意味着有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2023-03-12
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多