看完郭大神的第一行代码,自己开发一个小程序遇到困难,求业界大牛指点迷津
String provider;
private Handler handler = new Handler(){
public void handleMessage(Message msg){
String response =(String) msg.obj;
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(response+"ceshi");
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//LocationTask1 tsk = new LocationTask1();
//tsk.execute();
new Thread(new Runnable(){
public void run(){
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providerList = locationManager.getProviders(true);
if(providerList.contains(LocationManager.NETWORK_PROVIDER)){
provider= LocationManager.NETWORK_PROVIDER;
}
else{
Toast.makeText(MainActivity.this,"No Provider use",Toast.LENGTH_SHORT).show();
return;//根据Toast提醒用户,在之后在判断Provier开启没有;
}
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
Double lat;
lat= location.getLatitude();
Message message = new Message();
message.obj=lat.toString();
handler.sendMessage(message);
}
//locationManager.requestLocationUpdates(provider,5000,1,locationListener);
}
}).start();
}为什么如果设备没有开启位置服务,程序就会闪退,解决的办法是什么,求各位大神支招!最好有详细原理