如何使用CommonWare获取Android的位置?

Sha*_*hax 0 android location geolocation broadcastreceiver android-service

有人可以共享使用Commonware jar库的android项目的工作代码.

我已经从https://github.com/alexbirkett/cwac-locpoll下载了代码

我非常喜欢及时获取位置的概念,但我无法使用此库完成它... plz帮助......

明显的条目

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<receiver android:name="com.commonsware.cwac.locpoll.LocationPoller" />
<service android:name="com.commonsware.cwac.locpoll.LocationPollerService" />
Run Code Online (Sandbox Code Playgroud)

活动中的代码

private void initLocationTracker()
    {
        try{
            alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            Intent intent = new Intent(this, LocationPoller.class);

            Bundle bundle = new Bundle();
            com.commonsware.cwac.locpoll.LocationPollerParameter locationPollerParameter =
                                                                        new LocationPollerParameter(bundle);

            locationPollerParameter.setIntentToBroadcastOnCompletion(new Intent(this, LocationReceiver.class));

            // try GPS connection now

            locationPollerParameter.setProviders(new String[]
                    {LocationManager.GPS_PROVIDER,LocationManager.NETWORK_PROVIDER});

            locationPollerParameter.setTimeout(60000);
            intent.putExtras(bundle);

            pendingIntent = PendingIntent.getBroadcast(this,0,intent,0);
            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),PERIOD,pendingIntent);


            Toast
                    .makeText(this,
                            "Location polling every 30 minutes begun",
                            Toast.LENGTH_LONG)
                    .show();
        }
        catch (Exception e)
        {
            String s = e.getMessage();
        }
    }
Run Code Online (Sandbox Code Playgroud)

谢谢

编辑: 代码工作正常但按预期执行但我无法在LocationReceiver中接收广播.

Com*_*are 5

首先,这不是"用于android项目的Commonware jar库".这是Alex Birkett的"用于android项目的Commonware jar库"的分支.

其次,如您链接到的网页上所述:

demo/子项目中,您将找到一个演示使用的示例活动LocationPoller.