启动后台位置服务不起作用

时间:2014-09-17 16:24:00

标签: android service gps geolocation background-process

我在使用Android的后台定位服务时遇到了一些问题。我开始使用此代码并根据我的需要对其进行了修改:https://gist.github.com/blackcj/20efe2ac885c7297a676

以下是LocationLoggerServiceManager的修改部分:

我更改了部分,因此我可以使用自己的广播手动启动此服务。

    public class LocationLoggerServiceManager extends BroadcastReceiver {

    private SharedPreferences mPrefs;
    public static final String TAG = "LocationLoggerServiceManager";
    @Override
    public void onReceive(Context context, Intent intent) {
        // Make sure we are getting the right intent
        if( "android.intent.action.BOOT_COMPLETED".equals(intent.getAction()) || "ftm.vem_game.services.LocationLoggerServiceManager".equals((intent.getAction()))) {
            boolean mUpdatesRequested = false;
            // Open the shared preferences
            mPrefs = context.getSharedPreferences("ftm.vem_game.shared_preferences",
                    Context.MODE_PRIVATE);
            /*
             * Get any previous setting for location updates
             * Gets "false" if an error occurs
             */
            if (mPrefs.contains("KEY_UPDATES_ON")) {
                mUpdatesRequested = mPrefs.getBoolean("KEY_UPDATES_ON", false);
            }
            if(mUpdatesRequested){
                //ComponentName comp = new ComponentName(context.getPackageName(), BackgroundLocationService.class.getName());
                //ComponentName service = context.startService(new Intent().setComponent(comp));

                Intent i = new Intent(context, BackgroundLocationService.class);
                ComponentName service = context.startService(i);

                if (null == service){
                    // something really wrong here
                    Log.e(TAG, "Could not start service BackgroundLocationService");
                }
            }

        } else {
            Log.e(TAG, "Received unexpected intent " + intent.toString());
        }
    }
}

以下是我在MainActivity类中发送Broadcast的部分:

                public static final String BROADCAST = "ftm.vem_game.services.LocationLoggerServiceManager";
                SharedPreferences sharedPref = getSharedPreferences("ftm.vem_game.shared_preferences", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putBoolean("KEY_UPDATES_ON", true);
                editor.commit();

                Intent intent = new Intent(BROADCAST);
                Bundle extras = new Bundle();
                extras.putString("send_data", "test");
                intent.putExtras(extras);
                sendBroadcast(intent);

在服务类上,我什么都没改变。问题是服务永远不会启动,在Log中它说“无法启动服务BackgroundLocationService”。而context.startService()每次都返回null。

我不知道自己做错了什么,或者在开始服务之前我想错过一些事情。

1 个答案:

答案 0 :(得分:1)

也许您忘了在AndroidManifest.xml中定义服务,如果是这样,只需将以下代码添加到您的Manifest中:

    <service
      android:name="com.examples.yourApp.BackgroundLocationService"
      android:icon="@drawable/ic_launcher"
      android:label="@string/service_name">
    </service>

如果您想在后台永久运行它,请查看以下链接: http://uncorkedstudios.com/blog/background-location-updates-on-android