2013年9月15日日曜日

BroadcastReceiver を使って位置情報取得 エンクロージング:GPS

BroadcastReceiver はプログラムから登録しているのでマニフェストでは指定していない。


package com.example.gpsintent2;

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

 private static final String TAG = "MainActivity";
 private static final String ACTION_LOCATION_UPDATE = "com.android.practice.ACTION_LOCATION_UPDATE";
 private LocationManager locationManager;
 private PendingIntent pendingIntent;
 private LocationUpdateReceiver receiv;
 
 public class LocationUpdateReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
   // TODO 自動生成されたメソッド・スタブ
   Log.i(TAG, "onReceive");

   String action = intent.getAction();

   if (action != null && action.equals(ACTION_LOCATION_UPDATE)) {
    final Location location = (Location) intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED);
    
    if (location == null) {
     return;
    }
    updateView(location);
   }
  }

  private void updateView(Location location) {
   // TODO 自動生成されたメソッド・スタブ

   TextView textView = (TextView) findViewById(R.id.textView1);
   textView.setText(String.valueOf(location.getLatitude()));
  }

 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Log.i(TAG, "************** onCreate");

  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

  IntentFilter filter = new IntentFilter();
  filter.addAction(ACTION_LOCATION_UPDATE);

  receiv = new LocationUpdateReceiver();
  registerReceiver(receiv, filter);

 }

 @Override
 protected void onResume() {
  super.onResume();

  Log.i(TAG, "onResume");

  Intent intent = new Intent();
  intent.setAction(ACTION_LOCATION_UPDATE);

  pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


  for (String providerName : locationManager.getAllProviders()) {
   if (locationManager.isProviderEnabled(providerName)) {
     Log.d(TAG, "Provider: " + providerName);
    // LocationManagerにPendingIntentを登録
    locationManager.requestLocationUpdates(providerName, 0, 0, pendingIntent );
   }
  }
 }

 @Override
 protected void onPause() {
  super.onPause();

  Log.i(TAG, "onPause");

  unregisterReceiver(receiv);
  locationManager.removeUpdates(pendingIntent);

 }

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

関連記事

0 件のコメント:

コメントを投稿