0

有人可以分享一个使用Commonware jar library for android项目的工作代码。

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

我真的很喜欢及时获取位置的概念,但我无法使用这个库完成它......请帮助......

清单条目

<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" />

活动中的代码

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();
        }
    }

谢谢

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

4

1 回答 1

5

首先,这不是“Android 项目的通用 jar 库”。那是 Alex Birkett 的“Android 项目的通用 jar 库”的分支。

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

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

于 2012-10-13T12:48:12.217 回答