用心学Android之Handler续篇
来源:互联网 发布:淘宝店铺导航怎么上传 编辑:程序博客网 时间:2024/05/17 07:56
在上一篇的结尾处,总结出了Handler机制也可以用于主线程/UI线程向子线程/工作线程发送消息,但是没有给出实践的例程。
为了满足“童鞋们”的需要,在这一篇中给出一个例子,顺便纠正上一篇中的一个错误。Looper释放资源的方法是在Activity的onDestroy()方法中调用线程Looper的quit()方法,而不是release()方法。
activity_main.xml代码
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:autonavi="http://schemas.android.com/apk/res/com.AMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/providersList"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.8">
</ListView>
<TextView
android:id="@+id/CurrnetLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java代码
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.amap.mapapi.core.GeoPoint;
import com.amap.mapapi.map.MapController;
import com.amap.mapapi.map.MapView;
public class MainActivity extends Activity {
private LocationManagerm_locationManager;
private LocationProviderm_locationProvider;
private Handlerm_mainHandler = new Handler() {
@Override
public voidhandleMessage(Message msg) {
// TODO 自动生成的方法存根
super.handleMessage(msg);
switch (msg.what){
case 0:
((TextView)findViewById(R.id.CurrnetLocation))
.setText((String)msg.obj);
break;
}
}
};
private Handlerm_addressParseThreadHandler;
private Thread m_addressParseThread= new Thread() {
@Override
public void run() {
// TODO 自动生成的方法存根
super.run();
Looper.prepare();
m_addressParseThreadHandler= new Handler() {
@Override
public voidhandleMessage(Message msg) {
//TODO 自动生成的方法存根
super.handleMessage(msg);
switch(msg.what) {
case0:
Messagesend_msg = m_mainHandler.obtainMessage();
send_msg.what= 0;
Strings="位置提供者:"+msg.getData().getString("ProviderName")+"\n";
s+=addressParse((Location)msg.obj);
send_msg.obj= s;
m_mainHandler.sendMessage(send_msg);
break;
}
}
};
Looper.loop();
}
};
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (m_locationManager== null)
m_locationManager= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
initProvidersList();
m_addressParseThread.start();
}
@Override
protected void onDestroy() {
// TODO 自动生成的方法存根
super.onDestroy();
m_addressParseThreadHandler.getLooper().quit();
}
// 初始化位置提供者列表
private voidinitProvidersList() {
// 列出可用的位置提供者
ListView providersList= (ListView) findViewById(R.id.providersList);
providersList.setAdapter(newArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
((LocationManager)getSystemService(Context.LOCATION_SERVICE))
.getProviders(true)));
providersList.setOnItemClickListener(newOnItemClickListener() {
@Override
public voidonItemClick(AdapterView<?> arg0, View arg1, int arg2,
longarg3) {
// TODO 自动生成的方法存根
//
if(m_locationManager != null) {
m_locationProvider= m_locationManager.getProvider(arg0
.getItemAtPosition(arg2).toString());
}
if(m_addressParseThreadHandler != null) {
Messagesend_msg = m_addressParseThreadHandler
.obtainMessage();
send_msg.what= 0;
send_msg.obj= getCurrentLocation(m_locationProvider);
//这里还用到了Bundle
Bundlebu=new Bundle();
bu.putString("ProviderName",m_locationProvider.getName());
send_msg.setData(bu);
m_addressParseThreadHandler.sendMessage(send_msg);
}
}
});
}
// 获得当前位置
private LocationgetCurrentLocation(LocationProvider lp) {
if (m_locationManager!= null && lp != null
&&m_locationManager.isProviderEnabled(lp.getName())) {
returnm_locationManager.getLastKnownLocation(lp.getName());
} else {
return null;
}
}
// 解析地址信息
private StringaddressParse(Location location) {
String latLongString ="";
if (location != null) {
latLongString +="当前位置提供者是:" + location.getProvider() + "\n";
double lat =location.getLatitude();
double lng =location.getLongitude();
latLongString ="纬度:" + lat + "\n" + "经度:" + lng +"\n";
List<Address>addList = null;
Geocoder ge = newGeocoder(this);
try {
addList =ge.getFromLocation(lat, lng, 1);
} catch(IOException e) {
// TODOAuto-generated catch block
e.printStackTrace();
}
if (addList !=null && addList.size() > 0) {
for (int i =0; i < addList.size(); i++) {
Addressad = addList.get(i);
latLongString+= ad.getCountryName() + ";"
+ad.getLocality();
if (i!= 0) {
latLongString+= "\n";
}
}
}
} else {
latLongString +="无法获取位置信息";
}
return latLongString;
}
@Override
public boolean onCreateOptionsMenu(Menumenu) {
// Inflate the menu;this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
}- 用心学Android之Handler续篇
- 用心学Android之Handler
- 用心学Android之原创CoolTab(支持左右滑动、可无限加页)
- Android开发入门之路(续篇)
- Android开发入门之路(续篇)
- Android开发入门之路(续篇)
- 用心学Android之Bitmap缓存编,可为ListView等控件提供异步加载图片解决方案
- 零基础学Android源码之Handler机制
- 处女男学Android(四)---Handler进阶篇之Handler、Looper、MessageQueue工作机制
- 处女男学Android(五)---Handler完结篇之Handler的post方法详解
- Android ThreadLocal理解--续篇
- 学会批处理,用心学很容易!
- 学会批处理,用心学很容易
- 学会批处理,用心学很容易!
- 学会批处理,用心学很容易
- Django(python)用心学
- 处女男学Android(二)---Handler预习篇之Android的线程与UI
- 用心思考之感悟
- Android 高手进阶教程(十三)之----Android 数据库SQLiteDatabase的使用!!
- 雾非雾
- Servlet传输二进制
- 对 Windows 窗体控件进行线程安全调用
- 比赛将至
- 用心学Android之Handler续篇
- 控制台 - 网络管理之华为交换机经典问题汇总
- Java实现多态例子
- Spark SQL组件源码分析
- Android TextView字体加粗与改变字体颜色颜色
- ubuntu 13.10编译Linux内核2.6.14 启动发生错误
- 推荐网站
- 版本控制
- linux下mongodb的安装启动操作