进程:无法使用Intent启动com.google.android.gms.checkin.CheckinService服务

时间:2014-06-09 03:09:53

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

我正在尝试构建Google地图应用程序,但继续在我的LogCat中接收此应用程序。我在清单中设置了所有权限和元数据,但仍然对此错误感到茫然。已针对此特定错误查找了SO,但未发现与com.google.android.gms.checkin

相关的任何内容

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

问题在于,当我启动应用程序时,我收到此消息:

enter image description here

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

此外,LocationManager lm = = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);课程中的GPSTrack始终返回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>

修改

<application />

内添加了此内容
<service
        android:name="com.app.GPSTrack"
        android:exported="false"
        android:enabled="true"
        android:label="@string/service_name" >
    </service>

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

2 个答案:

答案 0 :(得分:0)

您应该在申请清单文件中注册Service课程,即GPSTracker课程:

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

添加<permission>代码:

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

P.S:如果您需要参考,可以按照link

进行操作

答案 1 :(得分:0)

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

<强>更新

这对我有用:

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地图