由网友(自认与酒同醉)分享简介:我正在开发中,我想找出用户的当前位置利用网络提供一个小的Android应用程序。我在以下几个方面尝试过这一点,但它没有给我任何的输出:networklocationManager =(LocationManager)本.getSystemService(Context.LOCATION_SERVICE);//定义...![学不在多而在精,2019继续深耕Android](/d/file/2023/09-17/f8f94ab03041ed310d9f95c4d64f7d25.png)
我正在开发中,我想找出用户的当前位置利用网络提供一个小的Android应用程序。我在以下几个方面尝试过这一点,但它没有给我任何的输出:
networklocationManager =(LocationManager)本
.getSystemService(Context.LOCATION_SERVICE);
//定义一个监听器,响应位置更新
LocationListener的networklocationListener =新LocationListener的(){
公共无效onLocationChanged(位置定位){
Log.i(*********,
这是我的网络位置+位置);
字符串Location_text =网络位置纬度:
+ location.getLatitude()+经度:
+ location.getLatitude();
network_location.setText(Location_text);
}
公共无效onStatusChanged(字符串商,INT地位,
捆绑演员){}
公共无效onProviderEnabled(字符串提供商){}
公共无效onProviderDisabled(字符串提供商){}
};
//注册监听器的位置管理器接收位置
//更新
networklocationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,
networklocationListener);
我在清单文件中像这样给权限
<使用,许可
机器人:名称=android.permission.INTERNET对/>
<使用,许可
机器人:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用,许可
机器人:名称=android.permission.ACCESS_FINE_LOCATION/>
有哪些我缺少什么东西?这是正确的方法是什么?需要帮忙。谢谢你...
公共类MainActivity扩展活动实现LocationListener的{
私人TextView的latituteField;
私人TextView的longitudeField;
私人LocationManager gpslocationManager;
私人LocationManager networklocationManager;
私人LocationManager networklocationManager1;
私人字符串提供商;
私人TextView的gps_location;
私人TextView的network_location;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
gps_location =(TextView中)findViewById(R.id.gps_location);
network_location =(TextView中)findViewById(R.id.network_location);
networkLocation();
}
公共无效networkLocation(){
networklocationManager =(LocationManager)本
.getSystemService(Context.LOCATION_SERVICE);
LocationListener的networklocationListener =新LocationListener的(){
公共无效onLocationChanged(位置定位){
Log.i(*********,
这是我的网络位置+位置);
字符串Location_text =网络位置纬度:
+ location.getLatitude()+经度:
+ location.getLatitude();
network_location.setText(Location_text);
}
公共无效onStatusChanged(字符串商,INT地位,
捆绑演员){}
公共无效onProviderEnabled(字符串提供商){}
公共无效onProviderDisabled(字符串提供商){}
};
networklocationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,
networklocationListener);
}
}
![学不在多而在精,2019继续深耕Android](/d/file/2023/09-17/f8f94ab03041ed310d9f95c4d64f7d25.png)
解决方案
已启用您的网络供应商?
布尔network_enabled;
尝试 {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}赶上(例外前){
ex.printStackTrace();
}
I am developing a small android application in which I want to find out the user's current location by using the network provider. I tried this in following ways but it's not giving me any output :
networklocationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener networklocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("********************************",
"this is my network location " + location);
String Location_text = "NETWORK LOCATION latitude:"
+ location.getLatitude() + " longitude:"
+ location.getLatitude();
network_location.setText(Location_text);
}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location
// updates
networklocationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
networklocationListener);
I gave permissions in my manifest file like this
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
Is there any thing which I am missing ? Is this the correct way? Need help. Thank you...
public class MainActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager gpslocationManager;
private LocationManager networklocationManager;
private LocationManager networklocationManager1;
private String provider;
private TextView gps_location;
private TextView network_location;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gps_location = (TextView) findViewById(R.id.gps_location);
network_location = (TextView) findViewById(R.id.network_location);
networkLocation();
}
public void networkLocation() {
networklocationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
LocationListener networklocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("********************************",
"this is my network location " + location);
String Location_text = "NETWORK LOCATION latitude:"
+ location.getLatitude() + " longitude:"
+ location.getLatitude();
network_location.setText(Location_text);
}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
networklocationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
networklocationListener);
}
}
解决方案
Is your network provider enabled?
boolean network_enabled;
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {
ex.printStackTrace();
}
相关推荐
最新文章