中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android GPS定位

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class GPSActivity extends Activity {
    //聲明位置管理對象
     private LocationManager locationManager;
     //聲明位置監(jiān)聽對象
     private LocationListener locationListener;
     //聲明字符串變量
     String locationprovider;
     //聲明顯示文本視圖組建
     private TextView textview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //獲得文本視圖
        textview = (TextView)this.findViewById(R.id.textView1);
        try{
            //新建Criteria類
            Criteria locationcriteria = new Criteria();
            //設(shè)置精確精度
            locationcriteria.setAccuracy(Criteria.ACCURACY_FINE);
            //不提供海拔高度信息
            locationcriteria.setAltitudeRequired(false);
            //不提供方向信息
            locationcriteria.setBearingRequired(false);
            //允許運(yùn)營商計(jì)費(fèi)
            locationcriteria.setCostAllowed(true);
            //設(shè)置電池消耗為低耗費(fèi)
            locationcriteria.setPowerRequirement(Criteria.POWER_LOW);
            //使用getSystemService()方法獲得位置管理器對象
            locationManager
            =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
            //locationManager.setTestProviderEnabled("gps", true);
            Toast.makeText(GPSActivity.this, "getSystemService", Toast.LENGTH_SHORT).show();
            //檢查gps功能開啟
            if(checkgps()){
                locationprovider 
                =locationManager.getBestProvider(locationcriteria, true);
                Log.d("provider", locationprovider);
                //注冊位置監(jiān)聽器
                locationListener = new MyLocationListener();
                locationManager.requestLocationUpdates(locationprovider, 1000, 0,locationListener);
            }
        }catch(Exception e){
            Toast.makeText(GPSActivity.this, "異常錯(cuò)誤"+e.toString(),Toast.LENGTH_LONG).show();
        }

    }
    private class MyLocationListener implements LocationListener{
         /**
          * 若位置發(fā)生變化,onLocationChanged方法被調(diào)用
          */
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            Log.i("位置發(fā)生變化", "Invoke");
            if(location != null){
                //獲得經(jīng)度
                String latitude = Double.toString(location.getLatitude());//經(jīng)度

                //獲得緯度

               String longitude = Double.toString(location.getLongitude());//緯度
                //在文本框中顯示
               textview = (TextView)GPSActivity.this.findViewById(R.id.textView1);
                textview.setText("經(jīng)度:"+longitude+"緯度"+latitude);
            }
            //locationManager.removeUpdates(this);
            //locationManager.setTestProviderEnabled("gps", true);
        }
         //若屏蔽提供商,該方法被調(diào)用

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
            Log.i("屏蔽提供商", "Invode");
        }
        //若激活提供商,該方法被調(diào)用
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
            Log.i("激活提供商", "Invode");
        }
       //若狀態(tài)發(fā)生變化,該方法被調(diào)用
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
            Log.i("狀態(tài)發(fā)生變化", "Invode");
        }

    }
    private boolean checkgps(){
        boolean providerEnabled
         = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        //若被激活,則返回真值
        if(providerEnabled ==true){
            Toast.makeText(this, "Gps模塊活動(dòng)正常", Toast.LENGTH_SHORT).show();
            return true;
        }
        else{
            Toast.makeText(this, "請開啟GPS", Toast.LENGTH_SHORT);
            return false;
        }

    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.antking.gps"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GPSActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:iOS隱藏鍵盤的代碼

下一篇:iOS從遠(yuǎn)程地址獲取圖片并修改尺寸