- package cn.domain.hello.activity;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.abc.R;
import cn.domain.hello.config.Config;
import cn.domain.hello.util.WebUtil;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.audiofx.BassBoost.Settings;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
private ViewGroup vsProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.etUsername = (EditText) this.findViewById(R.id.RepName);
this.etPassword = (EditText) this.findViewById(R.id.RepPassword);
this.btnLogin = (Button) this.findViewById(R.id.login);
this.btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String RepName = MainActivity.this.etUsername.getText()
.toString().trim();
String RepPassword = MainActivity.this.etPassword.getText()
.toString().trim();
if ("".equals(RepName)) {
Toast.makeText(MainActivity.this, "请填写用户名!",
Toast.LENGTH_SHORT).show();
return;
}
if ("".equals(RepPassword)) {
Toast.makeText(MainActivity.this, "请填写密码!",
Toast.LENGTH_SHORT).show();
return;
}
executeLogin(RepName, RepPassword);
}
});
}
private void executeLogin(String RepName, String RepPassword) {
new LoginTask().execute(RepName, RepPassword);
}
private void onLoginComplete(Integer RepID) {
if (RepID == null || RepID == 0) {
Toast.makeText(MainActivity.this, "登录名或密码错误", Toast.LENGTH_SHORT)
.show();
if (vsProgress != null) {
vsProgress.setVisibility(View.INVISIBLE);
}
return;
}
if (vsProgress != null) {
vsProgress.setVisibility(View.INVISIBLE);
}
System.out.println("+++++++++"+RepID);
Log.i("id", "RepID");
SharedPreferences setid=getSharedPreferences("setid", MODE_PRIVATE);
SharedPreferences.Editor editor=setid.edit();
editor.putInt("setid", RepID);
editor.commit();
Toast.makeText(MainActivity.this, "登录成功,等待页面跳转", Toast.LENGTH_SHORT).show();
LoginActivity.actionStart(MainActivity.this, RepID, etUsername
.getText().toString());
}
private class LoginTask extends AsyncTask<String, Void, Integer> {
protected void onPreExecute() {
super.onPreExecute();
if (vsProgress == null) {
ViewStub vs = (ViewStub) findViewById(R.id.vsProgress);
vsProgress = (ViewGroup) vs.inflate();
} else {
vsProgress.setVisibility(View.VISIBLE);
}
}
@Override
protected Integer doInBackground(String... params) {
Integer result = null;
JSONArray reqValue;
try {
reqValue = new JSONArray().put(new JSONObject().put("RepName",
params[0]).put("RepPassword", params[1]));
JSONArray rec = WebUtil.getJSONArrayByWeb(Config.METHOD_LOGIN,
reqValue);
if (rec != null) {
result = rec.getJSONObject(0).getInt("RepID");
}
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
onLoginComplete(result);
}
}
/*
* 绑定别名
*/
private static final int GET_MEG_SUG=0;
private String TAG="MainActivity";
private static final int MSG_SET_ALIAS=1001;
private Handler handler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
SharedPreferences setid=getSharedPreferences("setid", MODE_PRIVATE);
Integer userid=setid.getInt("userid", 0);
switch (msg.what) {
case GET_MEG_SUG:
Log.i(TAG,"接收广播,获取数据");
setAlias(userid);
break;
default:
break;
}
}
};
/**
* Jpush设置
*/
private void setAlias(int userid) {
String alias = String.valueOf(userid);
// 调用JPush API设置Alias
mHandler1.sendMessage(mHandler1.obtainMessage(MSG_SET_ALIAS, alias));
Log.d(TAG, "设置Jpush推送的别名alias=" + alias);
}
private final Handler mHandler1 = new Handler() {//专门用了一个Handler对象处理别名的注册问题
@Override
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
Log.d(TAG, "设置激光推送的别名-mHandler1");
JPushInterface.setAliasAndTags(getApplicationContext(),
(String) msg.obj, null, mAliasCallback);
}
};
private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
@Override
public void gotResult(int code, String alias, Set<String> tags) {
String logs;
switch (code) {
case 0:
logs = "Set tag and alias success极光推送别名设置成功";
Log.i(TAG, logs);
break;
case 6002:
logs = "Failed to set alias and tags due to timeout. Try again after 60s.极光推送别名设置失败,60秒后重试";
Log.i(TAG, logs);
mHandler1.sendMessageDelayed(mHandler1.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
break;
default:
logs = "极光推送设置失败,Failed with errorCode = " + code;
Log.e(TAG, logs);
break;
}
}
};
/*
* 接收信息
*/
public static boolean isForeground = false;
public static final String MESSAGE_RECEIVED_ACTION = "com.example.abc.MESSAGE_RECEIVED_ACTION";
public static final String KEY_MESSAGE = "message";
public static final String KEY_EXTRAS = "extras";
private MessageReceiver mMessageReceiver;
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
String messge = intent.getStringExtra(KEY_MESSAGE);
String extras = intent.getStringExtra(KEY_EXTRAS);
StringBuilder showMsg = new StringBuilder();
showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
}
}
}
private void init(){
JPushInterface.init(getApplicationContext());
}
@Override
protected void onResume() {
isForeground = true;
super.onResume();
}
@Override
protected void onPause() {
isForeground = false;
super.onPause();
}
@Override
protected void onDestroy() {
unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
}