ウェブアプリケーション,インジェクション,コマンドインジェクション

Androidアプリ開発 位置情報を扱うサービスいろいろ

BroadcastReceiverを使って位置情報を習得

Using a BroadCastReceiver to Obtain Locations

LocationListenerクラスを位置データを取得するために使うよりも
むしろ、BroadcatReceiverクラスと一緒にPendingIntentオブジェクトを
代わりに使うことができます。
このアプローチを使うと、たとえユーザがアプリケーションを
閉じたとしても位置データを見守り続けられます。
地理的位置情報をGPSなどを使って取得する
LocationListenerクラスのインスタンスを使って地理的位置情報を
取得する方法を示しました。
LocationListenerクラスはアクティビティ内から位置を
モニタしている場合には役に立ちます。
しかし、アクティビティがダメになっていれば、LocationListenerもまた、
ダメになっているでしょう。そして、
当然ですが、位置情報の追跡もまるで出来なくなります。
位置情報を得る他の方法は、PendingIntentオブジェクトと共に、
BroadcastReceiverクラスを作ることです。
どのように動くかを確認するために、パッケージにクラスを作り、
MyLocationReceiver.javaと名づけます。
MyLocationReceiver.javaの内容は以下のようにします。

package net.learn2develop.lbsreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.widget.Toast;

public class MyLocationReceiver extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent){
   String locationKey = LocationManager.KEY_LOCATION_CHANGED;
   String providerEnableKey = LocationManager.KEY_PROVIDER_ENABLED;
   if(intent.hasExtra(providerEnabledKey)){
    if(!intent.getBooleanExtra(providerEnabledKey, true)){
     Toast.makeText(context, "Provider enabled", Toast.LENGTH_SHORT).show();
    }else{
     Toast.makeText(context, "Provider enabled", Toast.LENGTH_SHORT).show();
    }
   }

   if(intent.hasExtra(locationKey)){
    Location loc = (Location)intent.getExtras().get(locationKey);
    Toast.makeText(context, "Location chaged : Lat: " +
      loc.getLatitude() + "Lng: " L loc.getLongitude(),
      Toast.LENGTH_SHORT).show();
   }
  }
}

BroadcastReceiverクラスのonReceive()メソッドが位置が
変更があったため停止されたら、そのIntent引数は位置情報を格納します。
LocationManager.KEY_PROVIDER_ENABLEDと
LocationManager.KEY_LOCATION_CHANGEDのキーを使って
確認させられます。この2つのキーは位置情報を示し、
大抵は、位置プロバイダを利用できるかどうかを示します。
位置データを習得するためには、今作ったばかりの
BroadcastReceiverクラスを使っていきます。
以下のコードをアクティビティに加えます。

package net.learn2develop.lbsreceiver;

import android.app.Activity;
import android.app.PendingTntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;

public class MainActivity extends Activity{
   LocationManager lm;
  PendingIntent pendingIntent;

  @Override
  public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   //---use the LocationManager class to obtain locations data---
   lm = (LocationManager)
    getSystemService(Context.LOCATION_SERVICE);
   Intent i = new Intent(this, MyLocationReceiver.class);
   pendingIntent = PendingIntent.getBroadcast(this, 0, 1,
      PendingIntent.FLAG_UPDATE_CURRENT);

   //---request for location updates using GPS---
   lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
       60000,
       100,
       pendingIntent);
  }
}

GPS、Wi-Fiなどを使って地理的位置情報を習得するのように
LocationManagerオブジェクトのrequestLocationUpdates()メソッドを
位置情報の更新をリクエストするために使います。
しかし、GPS、Wi-Fiなどを使って・・・の方法と違うのは、
LocationListenerオブジェクトの代わりにPendingIntentオブジェクトに
情報を渡していることです。
位置情報の変更があったときに、BroadcastReceiverのonReceive()
メソッドを始動します。
最後にAndroidManifest.xmlファイルに<receiver>要素を
追加するのをわすれないように

<manifest cmlns:android="http://schemas.android.com/apk/res/android"
  package="net.learn2develop.lbsreceiver"
  android:versionCode="1"
  android:VersionName="1.0" >

  <uses-sdk
   android:minSdkVersion="8"
   android:targetSdkVersion="15" />

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

  <application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <activity
    android:name=".MainActivity"
    android:label="@string/title_activity_main" >
    <intent-filter>
     <action android:name="android.intent.action.MAIN" />

     <category android:name="android.intent.categoty.LAUNCHER" />
    </intent-filter>
   </activity>

   <receiver android:name=".MyLocationReceiver" />
  </application>
</manifest>

これで、開発するアプリケーションは位置情報の更新を受け続けます。
アクティビティが終了されたとしても受け続けられます。


ちょっと注釈

バッテリーの保持のことを考えて、
位置情報の更新間隔を60,000ミリ秒と範囲100メートルにセットしてます。
それは、最新の更新を受け取ってから少なくても1分、最新の位置情報から
最低100メートル移動しない限り他の位置情報の更新を受けないことに
なります。このため、エミュレータでアプリケーションをテストするなら、
この事を踏まえて、更新の間にDDMSの緯度、経度を変更します。


PewndingIntentオブジェクトを参照し続けることで、位置追跡が、
終わったときに下記のコードを使って位置情報の更新を削除できます。

lm.removeUpdates(pendingIntent);
ホーム
便利堂ロゴ
inserted by FC2 system