Internet连接不签入应用程序

时间:2015-09-01 05:29:53

标签: android security android-manifest

我想在我的应用程序中集成Web服务,我在下面编写代码

Main.java

public class ActivityLogin extends Activity {

ProgressDialog mProgressDialog;
TextView mTextViewSignUp;
PostParseGet mPostParseGet;
GetSecrateKey mGetSecrateKey;
AllMethods mAllMethods;
public     String MY_PREF = "login";
ConnectivityManager mConnectivityManager;
NetworkInfo mNetworkInfo;
boolean isNetError;
EditText mEditTextUName;
EditText mEditTextPass;
TextView mTextViewLogin;
TextView mTextViewFG;

Tags mTags;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_login);
    mTextViewSignUp =(TextView)findViewById(R.id.txt_create);
      mPostParseGet=new PostParseGet(ActivityLogin.this);
      mGetSecrateKey=new GetSecrateKey();
      mAllMethods=new AllMethods(ActivityLogin.this);
      mEditTextUName=(EditText)findViewById(R.id.edt_uname);
      mEditTextPass=(EditText)findViewById(R.id.editText2);;
      mTextViewLogin=(TextView)findViewById(R.id.button1);;
      mTextViewFG=(TextView)findViewById(R.id.txt_fg);;
    mTags=new Tags();

    mTextViewLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mEditTextUName.getText().toString().equalsIgnoreCase(""))
            {
                mAllMethods.ShowDialog(ActivityLogin.this,"Validation Error","Enter email id.","OK");
            }
            else if (!mAllMethods.checkEmail(mEditTextUName.getText().toString()))
            {
                mAllMethods.ShowDialog(ActivityLogin.this,"Validation Error","Enter email id in proper form.","OK");

            }
           else  if (mEditTextPass.getText().toString().equalsIgnoreCase(""))
            {
                mAllMethods.ShowDialog(ActivityLogin.this,"Validation Error","Enter password.","OK");

            }
            else
            {
                if (check_Internet()==true)
                {
                    new Login().execute();
                }
                else
                {
                    mAllMethods.ShowDialog(ActivityLogin.this,getString(R.string.val_error),getString(R.string.network_not_avl),"OK");

                }
            }

        }
    });

}
public class Login extends AsyncTask<Void,Void,Void>
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
          mProgressDialog=ProgressDialog.show(ActivityLogin.this,getString(R.string.wait),getString(R.string.load));
    }

    @Override
    protected Void doInBackground(Void... params) {
        mGetSecrateKey=(GetSecrateKey)mPostParseGet.login(mGetSecrateKey,mEditTextPass.getText().toString(),mEditTextPass.getText().toString()) ;
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        if (mProgressDialog!=null)
        {
            mProgressDialog.dismiss();
        }

    }
}

public boolean check_Internet() {
    boolean connected = false;
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {

        connected = true;
    }
    else
        connected = false;


return connected;
   }
 }

的Manifest.xml

 <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

当我运行上面的代码时,它给出了我在行

下面的错误

if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()== NetworkInfo.State.CONNECTED ||

09-01 01:24:08.438  15145-15145/rootways.android.pkg.pingexpress E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: rootways.android.pkg.pingexpress, PID: 15145
java.lang.SecurityException: ConnectivityService: Neither user 10064 nor current process has android.permission.ACCESS_NETWORK_STATE.
        at android.os.Parcel.readException(Parcel.java:1546)
        at android.os.Parcel.readException(Parcel.java:1499)
        at android.net.IConnectivityManager$Stub$Proxy.getNetworkInfo(IConnectivityManager.java:991)
        at android.net.ConnectivityManager.getNetworkInfo(ConnectivityManager.java:640)
        at pkg.android.rootways.pingexpress.ActivityLogin.check_Internet(ActivityLogin.java:136)
        at pkg.android.rootways.pingexpress.ActivityLogin$2.onClick(ActivityLogin.java:76)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

任何想法我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:0)

ConnectivityManager的getActiveNetworkInfo()方法返回一个NetworkInfo实例,表示它可以找到的第一个连接的网络接口,如果没有连接任何接口,则返回null。检查此方法是否返回null应足以判断是否有可用的Internet连接。

private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager 
      = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

您还需要:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在您的Android清单中。