android 手机定位小测试工程

来源:互联网 发布:手机拍视频软件 编辑:程序博客网 时间:2024/05/22 03:51

因为最近用到定位获取城市天气的功能,公司提供的天气的接口,特此做了一下定位的功能。处女作,有不好之处还请博友们多多指教。

以下是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接口,系统会提示一些重写的方法,此处只在一个方法里面写就成
@Overridepublic void onLocationChanged(AMapLocation arg0) {// TODO Auto-generated method stubif (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;@Overrideprotected 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);}@Overridepublic void onClick(View arg0) {if(arg0 == location){mAMapLocManager = LocationManagerProxy.getInstance(MainActivity.this);mAMapLocManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 5000, 10, MainActivity.this);tvLocation.setText("");}}@Overrideprotected void onPause() {super.onPause();if (mAMapLocManager != null) {mAMapLocManager.removeUpdates(this);}}@Overrideprotected void onDestroy() {if (mAMapLocManager != null) {mAMapLocManager.removeUpdates(this);mAMapLocManager.destory();}mAMapLocManager = null;super.onDestroy();}@Overridepublic void onLocationChanged(Location arg0) {// TODO Auto-generated method stub}@Overridepublic void onProviderDisabled(String arg0) {// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String arg0) {// TODO Auto-generated method stub}@Overridepublic void onStatusChanged(String arg0, int arg1, Bundle arg2) {// TODO Auto-generated method stub}@Overridepublic void onLocationChanged(AMapLocation arg0) {// TODO Auto-generated method stubif (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,大家从网上搜来下载吧。

    完工!




0 0
原创粉丝点击