基于百度云推送的实时通信客户端实现(一)

最近在做一个实时聊天软件的客户端,但是因为水平不足,自己写sorket会很蛋疼,于是采用了百度云推送的机制,使用百度官方设置TAG的方法来实现登陆,实时推送消息来实现聊天的过程,闲话不多说,下面一步一步来分析代码吧

配置的过程放到最后再说吧,我使用的是在官方创建工程之后直接下载的范例demo修改的

这样就=可以下载范例demo了

欢迎界面布局很简单,只有一个铺满全屏的RelativeLayout,贴一张背景图片就好了

1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2     xmlns:tools="http://schemas.android.com/tools"
3     android:id="@+id/login_layout"
4     android:layout_width="fill_parent"
5     android:layout_height="fill_parent"
6     android:background="@drawable/welcome_back"
7     tools:context=".Login" >
8 
9 </RelativeLayout>

在java代码中,除了固定的线程休眠以外,只有一个检测当前登录状态的函数

 1 package com.baidu.push.example;
 2 
 3 import com.tuisong.Login;
 4 import com.tuisong.R;
 5 import android.os.Bundle;
 6 import android.view.Window;
 7 import android.view.WindowManager;
 8 import android.app.Activity;
 9 import android.content.Context;
10 import android.content.Intent;
11 import android.content.SharedPreferences;
12 
13 public class Welcome extends Activity {
14     public SharedPreferences sp;
15     Context context;
16 
17     //线程休眠,与此同时,检测当前是否登录
18     public void Start() {
19 
20         new Thread() {
21             public void run() {
22                 try {
23                     Thread.sleep(5000);
24                 } catch (InterruptedException e) {
25                     e.printStackTrace();
26                 }
27                 if (check() == 0) {
28                     Intent intent = new Intent(Welcome.this, Login.class);
29                     startActivity(intent);
30                     finish();
31                 } else {
32                     Intent intent = new Intent(Welcome.this,
33                             PushDemoActivity.class);
34                     startActivity(intent);
35                     finish();
36                 }
37             }
38         }.start();
39     }
40 
41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
44         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
45                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
46         setContentView(R.layout.activity_welcome);
47         Start();
48     }
49     
50     //检测当前是否登录,登陆状态使用Sharedpreferences来保存当前状态
51     public int check() {
52         sp = this.getSharedPreferences("key", 0);
53         System.out.println("Logon " + sp.getString("Login", null));
54         if (sp.getString("Login", null) == null) {
55             return 0;
56         } else if (sp.getString("Login", null).equals("-1")) {
57             return 0;
58         } else {
59             return 1;
60         }
61     }
62 }

在欢迎界面写完之后,就该写登陆界面了,登陆界面因为简单,所以布局直接采取了拖控件的方式(虽然我知道这样不太好)

因为是一个简单的demo,所以登陆只是简单地使用了百度SDK提供的设置TAG的方法,并没有与服务器进行检测

代码如下

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/login_layout"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     android:background="@drawable/main_backgroud"
 7     tools:context=".Login" >
 8 
 9     <EditText
10         android:id="@+id/et_user"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:layout_alignBottom="@+id/bt_login"
14         android:layout_alignLeft="@+id/bt_login"
15         android:layout_marginBottom="83dp"
16         android:background="@drawable/main_input"
17         android:ems="10"
18         android:gravity="center"
19         android:hint="请输入工号"
20         android:textColor="#000000" >
21 
22         <requestFocus />
23     </EditText>
24 
25     <Button
26         android:id="@+id/bt_login"
27         android:layout_width="wrap_content"
28         android:layout_height="36dp"
29         android:layout_alignParentTop="true"
30         android:layout_centerHorizontal="true"
31         android:layout_marginTop="198dp"
32         android:background="@drawable/main_login_btn"
33         android:gravity="center"
34         android:text="   登       陆  " />
35 
36     <ImageView
37         android:id="@+id/login_show_img"
38         android:layout_width="wrap_content"
39         android:layout_height="wrap_content"
40         android:layout_above="@+id/et_user"
41         android:layout_alignParentLeft="true"
42         android:layout_marginBottom="24dp"
43         android:background="@drawable/login_text" />
44 
45 </RelativeLayout>

  1 package com.tuisong;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 import android.app.Activity;
  7 import android.content.Context;
  8 import android.content.Intent;
  9 import android.content.SharedPreferences;
 10 import android.os.Bundle;
 11 import android.view.View;
 12 import android.view.View.OnClickListener;
 13 import android.view.inputmethod.InputMethodManager;
 14 import android.widget.Button;
 15 import android.widget.EditText;
 16 import android.widget.RelativeLayout;
 17 
 18 import com.baidu.android.pushservice.PushConstants;
 19 import com.baidu.android.pushservice.PushManager;
 20 import com.baidu.push.example.PushDemoActivity;
 21 import com.baidu.push.example.Utils;
 22 
 23 public class Login extends Activity {
 24     String User;
 25     Button bt;
 26     EditText et;
 27     SharedPreferences sp;
 28     SharedPreferences.Editor edtior;
 29     RelativeLayout layout;
 30 
 31     @Override
 32     protected void onCreate(Bundle savedInstanceState) {
 33         super.onCreate(savedInstanceState);
 34         setContentView(R.layout.activity_login);
 35         layout = (RelativeLayout) findViewById(R.id.login_layout);
 36         et = (EditText) findViewById(R.id.et_user);
 37         bt = (Button) findViewById(R.id.bt_login);
 38         sp = getSharedPreferences("key", 0);
 39         edtior = sp.edit();
 40 
 41         bt.setOnClickListener(new OnClickListener() {
 42 
 43             @Override
 44             public void onClick(View arg0) {
 45                 // TODO Auto-generated method stub
 46                 User = et.getText().toString();
 47                 //把用户名传递出去
 48                 edtior.putString("toUser", User);
 49                 //保存登陆状态
 50                 edtior.putString("Login", User);
 51                 //用来标记当前的活动activity以及activity的状态
 52                 edtior.putString("SSP", User);
 53                 edtior.commit();
 54                 System.out.println("the login in login is "
 55                         + sp.getString("Login", null));
 56                 PushManager.startWork(getApplicationContext(),
 57                         PushConstants.LOGIN_TYPE_API_KEY, "api_key");
 58                 PushManager.startWork(getApplicationContext(),
 59                         PushConstants.LOGIN_TYPE_API_KEY,
 60                         Utils.getMetaValue(Login.this, "api_key"));
 61                 // 在这里设置标签
 62                 List<String> tags = getTagsList(User);
 63                 System.out.println("User  " + User);
 64                 PushManager.setTags(getApplicationContext(), tags);
 65                 Intent intent = new Intent(getApplicationContext(),
 66                         PushDemoActivity.class);
 67                 intent.putExtra("userid", User);
 68                 startActivity(intent);
 69                 finish();
 70             }
 71         });
 72         //为了实现点击空白地方隐藏输入框的效果,监听RelativeLayout的点击事件
 73         layout.setOnClickListener(new OnClickListener() {
 74 
 75             @Override
 76             public void onClick(View v) {
 77                 // TODO Auto-generated method stub
 78                 if (v.getId() == R.id.login_layout) {
 79                     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 80                     et.setCursorVisible(false);// 失去光标
 81                     imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
 82                 }
 83             }
 84         });
 85 
 86     }
 87     //使用百度提供的方法来实现
 88     private List<String> getTagsList(String originalText) {
 89 
 90         List<String> tags = new ArrayList<String>();
 91         int indexOfComma = originalText.indexOf(',');
 92         String tag;
 93         while (indexOfComma != -1) {
 94             tag = originalText.substring(0, indexOfComma);
 95             tags.add(tag);
 96 
 97             originalText = originalText.substring(indexOfComma + 1);
 98             indexOfComma = originalText.indexOf(',');
 99         }
100 
101         tags.add(originalText);
102         return tags;
103     }
104 }

接下来就进入了应用启动以后的对话列表界面,这个部分在第二篇里面总结

原文地址:https://www.cnblogs.com/cwr941012/p/3583671.html