在大声说这是一个重复的问题之前,请注意我已经浏览了以下文章/问题:一个;两个;三个;四个;5;6;7;搜索查询
Ok。现在,既然,希望我们确定我不是那么懒惰,只是愚蠢,我可以请求您的帮助:如何检查数据连接是否打开,如果没有提示用户打开数据服务或退出应用程序android ?!请考虑到我是Java/XML/Android的新手,如果可能的话,我将需要彻底的解释,因为到目前为止我所尝试的一切都不起作用(因为不完整的代码,过时的代码,糟糕的即兴创作和编程技能(当然是我的))。我也会满足于代码,但如果附有(一些)解释是更好的,因为这是我理解和学习的机会。
这是唯一的东西的"工作",直到现在(有点因为,它结束总是说"错误设置移动数据状态",即使我已经启用数据运行应用程序之前):
public class WelcomeActivity extends AppCompatActivity {
ImageView imageView;
public void setMobileDataState(boolean mobileDataEnabled)
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
if (null != setMobileDataEnabledMethod)
{
setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
}
}
catch (Exception ex)
{
//Log.e(TAG, "Error setting mobile data state", ex);
Context context = getApplicationContext();
Toast.makeText(context, "Error setting mobile data state", Toast.LENGTH_LONG).show();
}
}
public boolean getMobileDataState()
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
if (null != getMobileDataEnabledMethod)
{
boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
return mobileDataEnabled;
}
}
catch (Exception ex)
{
// Log.e(TAG, "Error getting mobile data state", ex);
Context context = getApplicationContext();
Toast.makeText(context, "Error getting mobile data state", Toast.LENGTH_LONG).show();
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
imageView = (ImageView) findViewById(R.id.coverSpin);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.welcome_animation);
imageView.setAnimation(animation);
getMobileDataState();
setMobileDataState(true);
........................................
这是我的应用程序的第一个活动,它是一个"欢迎"动画活动,在那里我想检查数据是否在,然后应用程序正常运行进一步,如果不是,给用户启用数据的可能性,如果,当(从设置)用户返回禁用数据然后应用程序不继续和退出…
感谢您的时间和理解(对于我的无技能和问题)。任何帮助都是感激的!!
我要感谢:vidulaJ和Jagadesha NH。结合他们的两个解决方案,我成功地完成了任务。我发布了我使用的解决方案,以防有人需要它。vidulaJ和Jagadesha NH再次感谢你们!
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;
import java.lang.reflect.Method;
public class WelcomeActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
imageView = (ImageView) findViewById(R.id.coverSpin);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.welcome_animation);
imageView.setAnimation(animation);
if(isMobileDataEnabled()){
//Mobile data is enabled and do whatever you want here
Toast.makeText(this, getString(R.string.sDataOKEnable), Toast.LENGTH_SHORT).show();
}
else{
//Mobile data is disabled here
new AlertDialog.Builder(this).setTitle(getString(R.string.sDataReqMes))
.setMessage(getString(R.string.sDataReqEnable)).setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// if user clicks ok then it will open network settings
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).show();
}}
private boolean isMobileDataEnabled(){
boolean mobileDataEnabled = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
try {
Class cmClass = Class.forName(cm.getClass().getName());
Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true);
mobileDataEnabled = (Boolean)method.invoke(cm);
} catch (Exception e) {
e.printStackTrace();
}
return mobileDataEnabled;
}
}
我仍然可以使用一些帮助直接打开数据启用/禁用活动/菜单,而不是一般设置活动…
试试这个
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import java.lang.reflect.Method;
public class WelcomeActivity extends AppCompatActivity{
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
imageView = (ImageView) findViewById(R.id.coverSpin);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.welcome_animation);
imageView.setAnimation(animation);
if(isMobileDataEnabled()){
//Mobile data is enabled and do whatever you want here
}
else{
//Mobile data is disabled here
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Error");
builder.setMessage("No mobile data connection detected.");
builder.setCancelable(false);
builder.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
private boolean isMobileDataEnabled(){
boolean mobileDataEnabled = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
try {
Class cmClass = Class.forName(cm.getClass().getName());
Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true);
mobileDataEnabled = (Boolean)method.invoke(cm);
} catch (Exception e) {
e.printStackTrace();
}
return mobileDataEnabled;
}
}
试试这个简单的程序,如果数据不在上面,它会提醒用户打开设置
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!isDataAvailable()) { //check if data is enabled or not
new AlertDialog.Builder(this).setTitle("Unable to connect")
.setMessage("Enable data?").setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// if user clicks ok then it will open network settings
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).show();
} else {
Toast.makeText(this, "DATA IS ON", Toast.LENGTH_SHORT).show();
}
}
private boolean isDataAvailable() {
// returns true or false based on whether data is enabled or not
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
}
请尝试一下
public boolean isNetworkAvailable(Context context) {
ConnectivityManager ConnectMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (ConnectMgr == null)
return false;
NetworkInfo NetInfo = ConnectMgr.getActiveNetworkInfo();
if (NetInfo == null)
return false;
if (!NetInfo.isConnected()) {
//Show pop up
}
}
不要忘记在清单中添加这些权限。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />