Skip to content

Commit

Permalink
新增后台登录成功和失败的notification
Browse files Browse the repository at this point in the history
  • Loading branch information
未来 committed Mar 30, 2016
1 parent 89a0de2 commit c9eca4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/com/future/nkwlan/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public void onClick(View v) {
public void onClick(DialogInterface dialog, int which) {
new Login().execute(user.uid, user.psw);
Toast.makeText(getApplicationContext(), "正在登录...",
Toast.LENGTH_LONG).show();
Toast.LENGTH_SHORT).show();
}
});
if (ssid.equals("NKU_WLAN")) {
new Login().execute(user.uid, user.psw);
Toast.makeText(getApplicationContext(), "正在登录...",
Toast.LENGTH_LONG).show();
Toast.LENGTH_SHORT).show();
} else {
builder.show();
}
Expand All @@ -103,7 +103,7 @@ public void onClick(DialogInterface dialog, int which) {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "正在注销...",
Toast.LENGTH_LONG).show();
Toast.LENGTH_SHORT).show();
new Logout().execute();
}
});
Expand Down Expand Up @@ -222,7 +222,7 @@ private void updateUI(NetworkInfo info) {
ssidTxt.setVisibility(View.GONE);
loginInfoTxt.setVisibility(View.GONE);
loginBtn.setText("刷新");
isNkuTxt.setText("不是NKU_WLAN环境,无法登录");
isNkuTxt.setText(R.string.not_intranet_cant_login);
isNkuTxt.setVisibility(View.VISIBLE);
wifiImg.setImageResource(R.drawable.wifi_disconnected);
break;
Expand Down
37 changes: 31 additions & 6 deletions app/src/main/java/service/NetworkChangeReceiver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand All @@ -8,8 +11,10 @@
import android.os.AsyncTask;
import android.util.Log;

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


Expand All @@ -21,20 +26,30 @@ public class NetworkChangeReceiver extends BroadcastReceiver {

private String userId;
private String pwd;
private Notification.Builder builder;
private NotificationManager notificationManager;
private Context context;

@Override
public void onReceive(Context context, Intent intent) {
Log.e("wlan", "onReceive");
this.context = context;
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
User user = NkCache.getAccount(context);
userId = user.uid;
pwd = user.psw;


notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
builder = new Notification.Builder(context).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getString(R.string.app_name));

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> {
Expand All @@ -56,7 +71,7 @@ protected void onPostExecute(com.future.nkwlan.NetworkInfo info) {
if (userId == null)
Log.e("wlan", "Receiver:UNLOGIN, and no user saved");
else {
Log.e("wlan", "Receiver:UNLOGIN, perfoming login" + userId + pwd);
Log.e("wlan", "Receiver:UNLOGIN, perfoming login with" + userId);
new Login().execute(userId, pwd);
}
break;
Expand All @@ -77,13 +92,23 @@ protected com.future.nkwlan.NetworkInfo doInBackground(String... params) {

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

builder.setWhen(System.currentTimeMillis() + 100);
switch (info.status) {
case com.future.nkwlan.NetworkInfo.ONLINE:
Log.e("wlan", "Receiver.login: login success. notify user this good news!");
builder.setContentText("已为你登录NKU_WLAN,enjoy it!")
.setTicker("已登录NKU_WLAN!")
.setAutoCancel(true);
notificationManager.notify(1, builder.getNotification());
break;
case com.future.nkwlan.NetworkInfo.UN_LOGIN:

Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentText("登录NKU_WLAN失败,请打开APP手动登录")
.setContentIntent(pendingIntent);
notificationManager.notify(1, builder.getNotification());
Log.e("wlan", "Receiver.login: 登录失败,请确认账号密码正确,且余额充足");
break;
case com.future.nkwlan.NetworkInfo.UNCONNECTED:
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
本月使用流量:%.3f GB<br/>
]]>
</string>
<string name="not_intranet_cant_login">不是NKU_WLAN环境,无法登录</string>

</resources>

0 comments on commit c9eca4b

Please sign in to comment.