zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    
    }
  • 相关阅读:
    Ubuntu 杂音 alsa*
    安装YouCompleteMe
    vimrc
    Linux Windows 修改键盘映射
    VMware Workstation+Linux+Xshell+Xftp+MySQL+SQLyog 配置
    leetcode Merge Intervals
    leetcode Remove Duplicates from Sorted Array II
    用栈实现二叉树的非递归中序遍历
    nth_element 测试程序
    Windows 程序设计
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3690738.html
Copyright © 2011-2022 走看看