基於百度雲推送的實時通信客戶端實現(一)


最近在做一個實時聊天軟件的客戶端,但是因為水平不足,自己寫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 }

 

 

接下來就進入了應用啟動以后的對話列表界面,這個部分在第二篇里面總結

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM