Android:isProviderEnabled(LocationManager.NETWORK_PROVIDER) 没有 work.Only isProviderEnabled(LocationManager.GPS_PROVIDER)
Android:isProviderEnabled(LocationManager.NETWORK_PROVIDER) doesn't work.Only isProviderEnabled(LocationManager.GPS_PROVIDER)
当我 运行 我的应用程序在我的手机上 phone 我可以看到只有 GPS_PROVIDER 在工作 properly.The NETWORK_PROVIDER 仅当 GPS_PROVIDER 被触发。
这是代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!lm.isProviderEnabled(locationProvider)) {
networkCheck();
}
// Get Location Manager and check for GPS & Network location services
if (!lm.isProviderEnabled(gpsProvider)) {
gpsCheck();
}
private void networkCheck() {
Toast.makeText(MainActivity.this, "NETWORK!!!!", Toast.LENGTH_SHORT);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View mView = getLayoutInflater().inflate(R.layout.activity_internet_access_alert_dialog, null);
Button positiveBtn = (Button) mView.findViewById(R.id.positiveNetworkBtn);
final Button negativeBtn = (Button) mView.findViewById(R.id.negativeNetworkBtn);
builder.setView(mView);
final AlertDialog networkDialog = builder.create();
positiveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
networkDialog.dismiss();
}
});
negativeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
networkDialog.setCancelable(false);
networkDialog.setCanceledOnTouchOutside(false);
networkDialog.show();
}
public void gpsCheck(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View mView = getLayoutInflater().inflate(R.layout.activity_gps_alert_dialog, null);
Button positiveBtn = (Button) mView.findViewById(R.id.positiveBtn);
final Button negativeBtn = (Button) mView.findViewById(R.id.negativeBtn);
builder.setView(mView);
final AlertDialog dialog = builder.create();
positiveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Show location settings when the user acknowledges the alert dialog
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
dialog.dismiss();
}
});
negativeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
我注意到如果在我的 phone 上启用了定位(GPS_ENABLED)并且网络数据也被启用,NETWORK_PROVIDER 不会弹出(这是正确的)但是如果启用了我的位置并且禁用了网络数据,NETWORK_PROVIDER 也不会弹出...有什么想法吗?
两者都是定位提供商的类型
NETWORK_PROVIDER
网络位置提供商的名称。
GPS_PROVIDER
GPS 位置提供商的名称。
我认为您正在考虑 NETWORK_PROVIDER 作为移动数据,不是吗?
禁用移动数据并不意味着您要禁用 LocationManager.NETWORK_PROVIDER
根据您在此处输入的代码来测试是否仅移动数据被打开ON/OFF或任何网络WIFI或移动数据 转 ON/OFF.
public class ConnectivityUtils {
// Returns TRUE any network mobile data or WIFI turned on otherwise returns FALSE
public static boolean isNetworkConnected(@NonNull Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
// Returns TRUE only if mobile data is turned on otherwise returns FALSE
public static boolean isMobileDataConnected(@NonNull Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return mobile != null && mobile.isConnectedOrConnecting();
}
}
当我 运行 我的应用程序在我的手机上 phone 我可以看到只有 GPS_PROVIDER 在工作 properly.The NETWORK_PROVIDER 仅当 GPS_PROVIDER 被触发。 这是代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!lm.isProviderEnabled(locationProvider)) {
networkCheck();
}
// Get Location Manager and check for GPS & Network location services
if (!lm.isProviderEnabled(gpsProvider)) {
gpsCheck();
}
private void networkCheck() {
Toast.makeText(MainActivity.this, "NETWORK!!!!", Toast.LENGTH_SHORT);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View mView = getLayoutInflater().inflate(R.layout.activity_internet_access_alert_dialog, null);
Button positiveBtn = (Button) mView.findViewById(R.id.positiveNetworkBtn);
final Button negativeBtn = (Button) mView.findViewById(R.id.negativeNetworkBtn);
builder.setView(mView);
final AlertDialog networkDialog = builder.create();
positiveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
networkDialog.dismiss();
}
});
negativeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
networkDialog.setCancelable(false);
networkDialog.setCanceledOnTouchOutside(false);
networkDialog.show();
}
public void gpsCheck(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View mView = getLayoutInflater().inflate(R.layout.activity_gps_alert_dialog, null);
Button positiveBtn = (Button) mView.findViewById(R.id.positiveBtn);
final Button negativeBtn = (Button) mView.findViewById(R.id.negativeBtn);
builder.setView(mView);
final AlertDialog dialog = builder.create();
positiveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Show location settings when the user acknowledges the alert dialog
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
dialog.dismiss();
}
});
negativeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
我注意到如果在我的 phone 上启用了定位(GPS_ENABLED)并且网络数据也被启用,NETWORK_PROVIDER 不会弹出(这是正确的)但是如果启用了我的位置并且禁用了网络数据,NETWORK_PROVIDER 也不会弹出...有什么想法吗?
两者都是定位提供商的类型
NETWORK_PROVIDER 网络位置提供商的名称。
GPS_PROVIDER GPS 位置提供商的名称。
我认为您正在考虑 NETWORK_PROVIDER 作为移动数据,不是吗? 禁用移动数据并不意味着您要禁用 LocationManager.NETWORK_PROVIDER
根据您在此处输入的代码来测试是否仅移动数据被打开ON/OFF或任何网络WIFI或移动数据 转 ON/OFF.
public class ConnectivityUtils {
// Returns TRUE any network mobile data or WIFI turned on otherwise returns FALSE
public static boolean isNetworkConnected(@NonNull Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
// Returns TRUE only if mobile data is turned on otherwise returns FALSE
public static boolean isMobileDataConnected(@NonNull Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return mobile != null && mobile.isConnectedOrConnecting();
}
}