android 手機定位小測試工程

因爲最近用到定位獲取城市天氣的功能,公司提供的天氣的接口,特此做了一下定位的功能。處女作,有不好之處還請博友們多多指教。

以下是xml裏面的代碼,僅僅有一個按鈕跟一個文本框,按鈕用來點擊,文本框用來顯示
    <Button
        android:id="@+id/location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="定位"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#cc00ff"
        android:layout_marginLeft="20dip"
        android:layout_toRightOf="@id/location"
        android:gravity="center"
        android:textSize="30sp" />
接下來的需要用到的權限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

應用裏面涉及到對AndroidManifest.xml文件裏的application中加一段代碼(顏色標註)
 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="9806db1ffe65f03908039f7be91b61b9" />

        <activity
            android:name="com.example.locationdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
最後是activity裏的代碼
實現AMapLocationListener接口,系統會提示一些重寫的方法,此處只在一個方法裏面寫就成
	@Override
	public void onLocationChanged(AMapLocation arg0) {
		// TODO Auto-generated method stub
		if (location != null) {
			String str =arg0.getCity().split("市")[0];		
			tvLocation.setText(str);
			Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
		}
	}
tvLocation就是我們上面提到的文本框,裏面顯示的str即我們所要的城市名。
一下是activity的全部代碼
public class MainActivity extends Activity implements OnClickListener,AMapLocationListener {

	private TextView tvLocation;
	private Button location;
	private LocationManagerProxy mAMapLocManager = null;

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

	private void initView() {
		location = (Button) findViewById(R.id.location);
		location.setOnClickListener(this);
		tvLocation = (TextView) findViewById(R.id.tv_location);
	}

	@Override
	public void onClick(View arg0) {
		if(arg0 == location){
			mAMapLocManager = LocationManagerProxy.getInstance(MainActivity.this);
			mAMapLocManager.requestLocationUpdates(
					LocationProviderProxy.AMapNetwork, 5000, 10, MainActivity.this);
			tvLocation.setText("");
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if (mAMapLocManager != null) {
			mAMapLocManager.removeUpdates(this);
		}
	}

	@Override
	protected void onDestroy() {
		if (mAMapLocManager != null) {
			mAMapLocManager.removeUpdates(this);
			mAMapLocManager.destory();
		}
		mAMapLocManager = null;
		super.onDestroy();
	}

	@Override
	public void onLocationChanged(Location arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLocationChanged(AMapLocation arg0) {
		// TODO Auto-generated method stub
		if (location != null) {
			String str =arg0.getCity().split("市")[0];		
			tvLocation.setText(str);
			Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
		}
	}

}

最後要提醒大家一點,需要架包!!!
包名Android_Location_V1.0.4.jar,大家從網上搜來下載吧。

    完工!




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章