android之location02

 

package com.example.mars_3300_location02;

import java.net.ContentHandler;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.location.Criteria;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
    private Button locationButton;
    private Button bestProviderButton;
    private LocationManager locationManager;

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

        locationButton = (Button) findViewById(R.id.locationButtonId);
        bestProviderButton = (Button) findViewById(R.id.bestProviderButtonId);

        locationButton.setOnClickListener(new ProvidersButtonListener());
        bestProviderButton.setOnClickListener(new BestProviderButtonListener());

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    private class ProvidersButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            // 获取所有支持的定位类型
            List<String> providers = locationManager.getAllProviders();
            for (String string : providers) {
                System.out.println(string);
            }
        }
    }

    private class BestProviderButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            // 通过条件搜索最合适的Provider
            // 生成一个Criteria对象
            Criteria criteria = new Criteria();
            // 设置查询条件
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setPowerRequirement(Criteria.ACCURACY_LOW);
            criteria.setAltitudeRequired(false);
            criteria.setSpeedRequired(false);
            criteria.setCostAllowed(false);
            String provider = locationManager.getBestProvider(criteria, false);
            System.out.println("best provider --->" + provider);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

 

android之location02,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。