snailcome 2016-08-11 01:58 采纳率: 33.3%
浏览 6625
已采纳

安卓的极光推送绑定登录名为别名进行推送,请大神传授洪荒之力!!!!

  1. 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();

}
}

  • 写回答

1条回答 默认 最新

  • Eiualee 2016-08-11 02:48
    关注

    你想问什么?感觉代码分的方法太多,看的好累,直接你都开了异步任务来处理登录业务了,登陆成功直接setAliasAndTags就行了啊,没什么必要用handler什么的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 6月28日

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?