Skip to content

Commit

Permalink
完成监听网络变化并登录
Browse files Browse the repository at this point in the history
  • Loading branch information
未来 committed Mar 28, 2016
1 parent 6f7a915 commit 89a0de2
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 26 deletions.
18 changes: 13 additions & 5 deletions app/src/main/java/com/future/nkwlan/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.future.nkwlan;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.wifi.WifiManager;
Expand Down Expand Up @@ -31,6 +32,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private TextView messageTxt, uidTxt, ssidTxt, isNkuTxt, loginInfoTxt;
private String ssid = "to be getting";
private String isNKU = "是";
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -39,7 +41,6 @@ protected void onCreate(Bundle savedInstanceState) {

User user = NkCache.getAccount(this);


messageTxt = (TextView) findViewById(R.id.main_message_txt);
uidTxt = (TextView) findViewById(R.id.main_uid_txt);
loginInfoTxt = (TextView) findViewById(R.id.main_login_info_txt);
Expand All @@ -50,8 +51,6 @@ protected void onCreate(Bundle savedInstanceState) {
loginBtn = (Button) findViewById(R.id.main_login_btn);
loginBtn.setOnClickListener(this);
// wifiImg.setOnClickListener(this);
WifiManager wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
ssid = wifiManager.getConnectionInfo().getSSID().replace("\"", "");

Log.e("nkwlan", "onCreate");
if (user.uid.length() == 0) {
Expand All @@ -62,6 +61,9 @@ protected void onCreate(Bundle savedInstanceState) {

public void onResume() {
super.onResume();
context = this;
WifiManager wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
ssid = wifiManager.getConnectionInfo().getSSID().replace("\"", "");
new UpdateStatus().execute();
Log.e("nkwlan", "onResume");
}
Expand All @@ -86,8 +88,13 @@ public void onClick(DialogInterface dialog, int which) {
Toast.LENGTH_LONG).show();
}
});
if (!ssid.equals("NKU_WLAN"))
if (ssid.equals("NKU_WLAN")) {
new Login().execute(user.uid, user.psw);
Toast.makeText(getApplicationContext(), "正在登录...",
Toast.LENGTH_LONG).show();
} else {
builder.show();
}
break;
case NetworkInfo.ONLINE:
builder.setMessage(Html.fromHtml(
Expand Down Expand Up @@ -145,11 +152,12 @@ public void onClick(DialogInterface dialog, int which) {
builder.show();
}


private class UpdateStatus extends AsyncTask<String, Long, NetworkInfo> {

@Override
protected NetworkInfo doInBackground(String... params) {
if (!NkNetwork.isWifi(context))
return new NetworkInfo(NetworkInfo.UNCONNECTED);
return NkNetwork.getLogStatus();
}

Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/com/future/nkwlan/NkNetwork.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.future.nkwlan;

import android.content.Context;
import android.net.ConnectivityManager;
import android.util.Log;

import com.squareup.okhttp.Call;
Expand Down Expand Up @@ -60,6 +62,7 @@ public static NetworkInfo getLogStatus() {

public static NetworkInfo login(String uid, String psw) {
NetworkInfo tmp = getLogStatus();
// Log.e("wlan", String.format("status check before login:%s,%s,%s",tmp.status,uid,psw));
if (tmp.status != NetworkInfo.UN_LOGIN)
return tmp;
RequestBody requestBody = RequestBody.create(
Expand All @@ -70,8 +73,9 @@ public static NetworkInfo login(String uid, String psw) {
Request request = new Request.Builder().post(requestBody).url(hosts[0] + login_path).build();
try {
String result = client.newCall(request).execute().body().string();
Log.e("future.wlan", result);
Log.e("wlan", result);
} catch (IOException e) {
Log.e("wlan", e.toString());
e.printStackTrace();
}
return getLogStatus();
Expand Down Expand Up @@ -118,4 +122,13 @@ private static String find(String raw, String tag) {
String raw2 = raw1.substring(0, raw1.indexOf("'"));
return raw2.trim();
}

public static boolean isWifi(Context context) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifiNetworkInfo =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return wifiNetworkInfo.isConnected();

}
}
84 changes: 74 additions & 10 deletions app/src/main/java/service/NetworkChangeReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,91 @@
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;

import com.future.nkwlan.NkCache;
import com.future.nkwlan.NkNetwork;
import com.future.nkwlan.User;


/**
* 接收网络状况变化的广播
* Created by future on 2016/3/28 0028.
*/
public class NetworkChangeReceiver extends BroadcastReceiver {

private String userId;
private String pwd;

@Override
public void onReceive(Context context, Intent intent) {
Log.e("wlan","onReceive");
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (activeNetInfo != null) {
Log.e("wlan", "NCR: Active Network Type : " + activeNetInfo.getTypeName());
// Toast.makeText(context, "Active Network Type : " +
// activeNetInfo.getTypeName(), Toast.LENGTH_SHORT).show();
Log.e("wlan", "onReceive");
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiInfo.isConnected()) {
new UpdateStatus().execute();
}
User user = NkCache.getAccount(context);
userId = user.uid;
pwd = user.psw;
}

private class UpdateStatus extends AsyncTask<String, Long, com.future.nkwlan.NetworkInfo> {

@Override
protected com.future.nkwlan.NetworkInfo doInBackground(String... params) {

return NkNetwork.getLogStatus();
}
if (mobNetInfo != null) {
Log.e("wlan", "NCR: Mobile Network Type : " + mobNetInfo.getTypeName());

@Override
protected void onPostExecute(com.future.nkwlan.NetworkInfo info) {
switch (info.status) {
case com.future.nkwlan.NetworkInfo.UNCONNECTED:

Log.e("wlan", "Receiver:UNCONNECTED, nothing to do");
break;
case com.future.nkwlan.NetworkInfo.UN_LOGIN://登录&保存
if (userId == null)
Log.e("wlan", "Receiver:UNLOGIN, and no user saved");
else {
Log.e("wlan", "Receiver:UNLOGIN, perfoming login" + userId + pwd);
new Login().execute(userId, pwd);
}
break;
case com.future.nkwlan.NetworkInfo.ONLINE:
Log.e("wlan", "Receiver:online, nothing to do");
break;
}
}
}

private class Login extends AsyncTask<String, Long, com.future.nkwlan.NetworkInfo> {

@Override
protected com.future.nkwlan.NetworkInfo doInBackground(String... params) {

return NkNetwork.login(params[0], params[1]);
}

@Override
protected void onPostExecute(com.future.nkwlan.NetworkInfo info) {

switch (info.status) {
case com.future.nkwlan.NetworkInfo.ONLINE:
Log.e("wlan", "Receiver.login: login success. notify user this good news!");
break;
case com.future.nkwlan.NetworkInfo.UN_LOGIN:

Log.e("wlan", "Receiver.login: 登录失败,请确认账号密码正确,且余额充足");
break;
case com.future.nkwlan.NetworkInfo.UNCONNECTED:
Log.e("wlan", "Receiver.login: 网络连接失败,弹一个通知让用户进APP内部操作");
break;
}
}
}
}
20 changes: 10 additions & 10 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
<resources>
<string name="app_name">NKU-Helper</string>
<string name="app_name">无线南开</string>

<string name="app_description">
<string name="app_description" translatable="false">
<![CDATA[
<p><b>使用帮助:</b></p>
输入你的NKU_WLAN用户名和密码,APP将会保存在本地,在当你连入NKU_WLAN时,APP将自动为你登录。
]]>
</string>

<string name="main_user_info" formatted="false">
<string name="main_user_info" formatted="false" translatable="false">
<![CDATA[
SSID:%s<br/>
校园网\t:\t%s<br/>
登录账号\t :\t%s <br/>
]]>
</string>

<string name="main_ssid" formatted="false">
<string name="main_ssid" formatted="false" translatable="false">
<![CDATA[
SSID : %s<br/>
]]>
</string>

<string name="main_uid" formatted="false">
<string name="main_uid" formatted="false" translatable="false">
<![CDATA[
登录账号 :%s<br/>
]]>
</string>

<string name="main_isnku" formatted="false">
<string name="main_isnku" formatted="false" translatable="false">
<![CDATA[
NKU校园网 :%s<br/>
]]>
</string>

<string name="main_logout_or_not" formatted="false">
<string name="main_logout_or_not" formatted="false" translatable="false">
<![CDATA[
你当前保存的账号为 :<b>%s</b><br/>是否更换账号?
]]>
</string>

<string name="main_login_alert" formatted="false">
<string name="main_login_alert" formatted="false" translatable="false">
<![CDATA[
是否使用 <b>%s</b> 登录 <b>%s</b> ?
]]>
</string>

<string name="main_logout_alert" formatted="false">
<string name="main_logout_alert" formatted="false" translatable="false">
<![CDATA[
是否注销 <b>%s</b> 在WiFi <b>%s</b> 的登录?
]]></string>

<string name="main_network_info" formatted="false">
<string name="main_network_info" formatted="false" translatable="false">
<![CDATA[
登录时长 :%.0f 分钟<br/>
账户余额 :%.2f 元<br/>
Expand Down

0 comments on commit 89a0de2

Please sign in to comment.