簡體   English   中英

啟動后台位置服務不起作用

[英]Starting Background Location Service not working

我在使用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。

我不知道自己做錯了什么,或者在開始服務之前我想錯過一些事情。

也許您忘了在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM