0

現在の場所を取得し、これらを に保存してBundleに転送する必要がありBroadCastReceiverます。これは 5 秒ごとにトリガーされます。これが私のコードの構造です

public class GPSServiceActivity extends Activity {

public Bundle locationBundle;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    locationBundle = new Bundle();
    Calendar cal = Calendar.getInstance();

    getCurrentLocation();

    Intent intent = new Intent(GPSServiceActivity.this, GPSHandler.class);
    intent.putExtra("Latitude", locationBundle.getDouble("Latitude"));
    intent.putExtra("Longitude", locationBundle.getDouble("Longitude"));
    // In reality, you would want to have a static variable for the request
    // code instead of 192837
    PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Get the AlarmManager service
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000,
            sender);
}

public void getCurrentLocation() {
    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
            5000, 0, mlocListener);

}

/* Class My Location Listener */
public class MyLocationListener implements LocationListener {



    public void onLocationChanged(Location loc) {

        double Latitude = loc.getLatitude();
        double Longitude = loc.getLongitude();

        locationBundle.putDouble("Longitude", Longitude);
        locationBundle.putDouble("Latitude", Latitude);

    }

    public void onProviderDisabled(String arg0) {

    }

    public void onProviderEnabled(String arg0) {

    }

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

    }
}

}

じぶんのManifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

しばらくコードをデバッグした後、値が 0.0 と 0.0 の 2 つの double が含まれているだけで、MyLocationListener決してトリガーされないことがわかりました。Bundle locationBundle誰かがこれを起動して実行する方法についてのヒントを教えてもらえますか?

編集

テスト目的で、このコードを書きました

List<String> providers = mlocManager.getAllProviders();

        if(providers.size() > 0) {
            for(int i = 0; i < providers.size(); i++) {
                Log.i("Providers: ", providers.get(i).toString());
            }
        }
        else
            Log.i("Providers: ", "No providers");

そして、これは出力します

network
gps
passive
4

2 に答える 2

0
MyLocationOverlay

それはGoogleマップAPIのメソッドです

于 2012-10-05T09:43:38.127 に答える
0

プロバイダーが有効になっているかどうかを確認できます。そうでない場合は、ユーザーに有効にするように依頼します。updateLocationRequest 呼び出しの最後の引数を確認してください。mlocListener とは何ですか? それはあなたのロケーションリスナークラス( MyLocationListener )のオブジェクトですか?

于 2012-10-05T09:24:38.037 に答える