啊啊啊啊啊啊啊啊,這東西越作越以爲是個深坑啊!java
首先仍是從主界面開始,由於要提高一下用戶體驗天然要加入保存密碼和自動登陸的功能。android
1 <CheckBox 2 android:text="保存密碼" 3 android:layout_weight="1" 4 android:id="@+id/save_password" 5 android:layout_width="wrap_content" 6 android:layout_height="wrap_content" /> 7 <CheckBox 8 android:text="自動登陸" 9 android:layout_weight="1" 10 android:id="@+id/auto_login_in" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" />
接着在主活動裏寫一下下:web
1 check_watch=(CheckBox)findViewById(R.id.show); 2 check_watch.setOnCheckedChangeListener(checkBox_Listener); 3 4 check_save=(CheckBox)findViewById(R.id.save_password); 5 check_save.setOnCheckedChangeListener(checkBox_Listener); 6 7 check_auto=(CheckBox)findViewById(R.id.auto_login_in); 8 check_auto.setOnCheckedChangeListener(checkBox_Listener);
1 private SharedPreferences sp; 2 public static SharedPreferences.Editor editor;
1 sp = this.getSharedPreferences("userInfo",Context.MODE_PRIVATE);//存儲密碼
1 private OnCheckedChangeListener checkBox_Listener = new OnCheckedChangeListener() {//全部checkbox的監聽器 2 @Override 3 public void onCheckedChanged(CompoundButton buttonView, 4 boolean isChecked) { 5 // TODO Auto-generated method stub 6 switch (buttonView.getId()) 7 { 8 case R.id.show: 9 if (isChecked) { 10 password.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); 11 } else { 12 password.setTransformationMethod(PasswordTransformationMethod.getInstance()); 13 } 14 break; 15 case R.id.save_password: 16 if (isChecked) { 17 sp.edit().putBoolean("ISCHECK", true).apply(); 18 } else { 19 sp.edit().putBoolean("ISCHECK", false).apply(); 20 } 21 break; 22 case R.id.auto_login_in: 23 if (isChecked) { 24 sp.edit().putBoolean("AUTO_ISCHECK", true).apply(); 25 } else { 26 sp.edit().putBoolean("AUTO_ISCHECK", false).apply(); 27 } 28 break; 29 } 30 } 31 }; 32 private void checkbox_init() {//checkbox判斷函數 33 //判斷記住密碼多選框的狀態 34 if(sp.getBoolean("ISCHECK", false)) 35 { 36 //設置默認是記錄密碼狀態 37 check_save.setChecked(true); 38 name.setText(sp.getString("USER_NAME","")); 39 password.setText(sp.getString("PASSWORD","")); 40 //判斷自動登錄多選框狀態 41 if(sp.getBoolean("AUTO_ISCHECK", false)) 42 { 43 //設置默認是自動登陸狀態 44 check_auto.setChecked(true); 45 //跳轉界面 46 //account=sp.getString("USER_NAME",""); 47 //pwd=sp.getString("PASSWORD",""); 48 Log.i("======================"+account,pwd+"==================================="); 49 accountLogin(); 50 } 51 } 52 } 53 private void setCheck_save(){ 54 if(check_save.isChecked()) 55 { 56 //記住用戶名、密碼、 57 editor = sp.edit(); 58 editor.putString("USER_NAME", account); 59 editor.putString("PASSWORD",pwd); 60 editor.apply(); 61 } 62 }
在onCreat()裏面每次都要判check_init()斷一下密碼的存儲狀態,而後再在每次登錄成功後運行setCheck_save()來保存密碼。OnCheckChangeListener()裏面設置checkbox的按下和彈起的狀態。app
在下面加入了兩個新的Button的用來註冊和訪問做者博客。ide
加入了一個放一個webview的活動:函數
1 package com.lfk.webim; 2 3 import android.os.Bundle; 4 import android.view.KeyEvent; 5 import android.view.View; 6 import android.webkit.WebSettings; 7 import android.webkit.WebView; 8 import android.webkit.WebViewClient; 9 import android.widget.ZoomButtonsController; 10 11 import com.lfk.webim.appli.BaseActivity; 12 13 import java.lang.reflect.Field; 14 15 16 public class AuthorBlog extends BaseActivity { 17 private WebView webView; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_author_blog); 22 webView=(WebView)findViewById(R.id.webview); 23 webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); //使滾動條不佔位 24 WebSettings webSettings =webView.getSettings(); 25 webSettings.setBuiltInZoomControls(true); 26 webView.setInitialScale(100);//縮放比例(徹底不縮放) 27 webView.getSettings().setDisplayZoomControls(false);//隱藏webview縮放按鈕 28 setZoomControlGone(webView); 29 webView.getSettings().setJavaScriptEnabled(true);//容許使用JS 30 webView.setWebViewClient(new WebViewClient() {//設置url 31 @Override 32 public boolean shouldOverrideUrlLoading(WebView view, String url) { 33 view.loadUrl(url); 34 return true; 35 } 36 }); 37 webView.loadUrl("http://www.cnblogs.com/lfk-dsk/"); 38 39 } 40 public void setZoomControlGone(View view) { 41 Class classType; 42 Field field; 43 try { 44 classType = WebView.class; 45 field = classType.getDeclaredField("mZoomButtonsController"); 46 field.setAccessible(true); 47 ZoomButtonsController mZoomButtonsController = new ZoomButtonsController(view); 48 mZoomButtonsController.getZoomControls().setVisibility(View.GONE); 49 try { 50 field.set(view, mZoomButtonsController); 51 } catch (IllegalArgumentException e) { 52 e.printStackTrace(); 53 } catch (IllegalAccessException e) { 54 e.printStackTrace(); 55 } 56 } catch (SecurityException e) { 57 e.printStackTrace(); 58 } catch (NoSuchFieldException e) { 59 e.printStackTrace(); 60 } 61 } 62 @Override 63 public boolean onKeyDown(int keyCode, KeyEvent event) {//設置返回鍵,返回鍵不直接回都上一個活動,而是放回webview的上一頁 64 if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) { 65 webView.goBack();// 返回前一個頁面 66 return true; 67 } 68 return super.onKeyDown(keyCode, event); 69 } 70 }
1 public static String regist(String account, String password) { 2 if (connect.getConnection() == null) 3 return "0"; 4 Registration reg = new Registration(); 5 reg.setType(IQ.Type.SET); 6 reg.setTo(connect.getConnection().getServiceName()); 7 reg.setUsername(account);// 注意這裏createAccount註冊時,參數是username,不是jid,是「@」前面的部分。 8 reg.setPassword(password); 9 reg.addAttribute("android", "geolo_createUser_android");// 這邊addAttribute不能爲空,不然出錯。因此作個標誌是android手機建立的吧!!!!! 10 PacketFilter filter = new AndFilter(new PacketIDFilter( 11 reg.getPacketID()), new PacketTypeFilter(IQ.class)); 12 PacketCollector collector = connect.getConnection() 13 .createPacketCollector(filter); 14 connect.getConnection().sendPacket(reg); 15 IQ result = (IQ) collector.nextResult(SmackConfiguration 16 .getPacketReplyTimeout()); 17 // Stop queuing results 18 collector.cancel();// 中止請求results(是否成功的結果) 19 if (result == null) { 20 Log.e("RegistActivity", "No response from server."); 21 return "no find on internet"; 22 } else if (result.getType() == IQ.Type.RESULT) { 23 return "regist success"; 24 } else { // if (result.getType() == IQ.Type.ERROR) 25 if (result.getError().toString().equalsIgnoreCase("conflict(409)")) { 26 Log.e("RegistActivity", "IQ.Type.ERROR: " 27 + result.getError().toString()); 28 return "this account has existed"; 29 } else { 30 Log.e("RegistActivity", "IQ.Type.ERROR: " 31 + result.getError().toString()); 32 return "regist failed"; 33 } 34 } 35 }
最好把這個函數加入工具類,接着寫一個和主界面差很少的界面用來註冊:工具
1 package com.lfk.webim; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.os.Message; 6 import android.text.method.HideReturnsTransformationMethod; 7 import android.text.method.PasswordTransformationMethod; 8 import android.util.Log; 9 import android.view.View; 10 import android.widget.CheckBox; 11 import android.widget.CompoundButton; 12 import android.widget.EditText; 13 import android.widget.Toast; 14 15 import com.lfk.webim.server.ConnecMethod; 16 import com.lfk.webim.server.connect; 17 18 19 public class regist extends Activity implements View.OnClickListener { 20 private EditText name,password; 21 private String nametemp,pwdtemp; 22 static int MSTTL=1; 23 static int MSTTLS=0; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_regist); 28 name=(EditText)findViewById(R.id.login_name_reg); 29 password=(EditText)findViewById(R.id.login_password_reg); 30 findViewById(R.id.buttonclear_reg).setOnClickListener(this); 31 findViewById(R.id.button_regist_reg).setOnClickListener(this); 32 CheckBox checkBox1=(CheckBox)findViewById(R.id.show_reg); 33 connect.getConnection(); 34 checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 35 @Override 36 public void onCheckedChanged(CompoundButton buttonView, 37 boolean isChecked) { 38 // TODO Auto-generated method stub 39 if (isChecked) { 40 password.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); 41 } else { 42 password.setTransformationMethod(PasswordTransformationMethod.getInstance()); 43 } 44 } 45 }); 46 } 47 private android.os.Handler RegHandler = new android.os.Handler() { 48 public void handleMessage(android.os.Message msg) { 49 switch (msg.what) { 50 case 1: 51 name.setText(""); 52 password.setText(""); 53 Log.d("+++++","======================="); 54 break; 55 case 0: 56 String temp= String.valueOf(msg.obj); 57 Toast.makeText(regist.this,temp,Toast.LENGTH_SHORT).show(); 58 default: 59 break; 60 } 61 } 62 }; 63 64 private void accountRegis() { 65 new Thread() { 66 public void run() { 67 String account = ((EditText) findViewById(R.id.login_name_reg)) 68 .getText().toString(); 69 String pwd = ((EditText) findViewById(R.id.login_password_reg)).getText() 70 .toString(); 71 String is = ConnecMethod.regist(account, pwd); 72 Message message=new Message(); 73 message.what=MSTTLS; 74 message.obj=is; 75 RegHandler.sendMessage(message); 76 } 77 }.start(); 78 } 79 @Override 80 public void onClick(View v) { 81 // TODO Auto-generated method stub 82 switch (v.getId()) { 83 case R.id.buttonclear_reg: 84 Message message=new Message(); 85 message.what=MSTTL; 86 RegHandler.sendMessage(message); 87 break; 88 case R.id.button_regist_reg: 89 Log.d("======================", "Reg"); 90 accountRegis(); 91 break; 92 default: 93 break; 94 } 95 } 96 97 }
另外加入了一個Clear的Button用來清理全部的東西讓用戶從新入。post
find方法尚未弄完,因此先說以前的。ui
加入了谷歌的下拉刷新來及時的更新數據:this
1 <android.support.v4.widget.SwipeRefreshLayout 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/swipe_refresh" 4 android:layout_below="@id/find_fri" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 <ListView 8 android:id="@+id/friend_list" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent"> 11 </ListView> 12 </android.support.v4.widget.SwipeRefreshLayout>
1 swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 2 3 @Override 4 public void onRefresh() { 5 new Handler().postDelayed(new Runnable() {//延遲跳轉=-= 6 public void run() { 7 swipeLayout.setRefreshing(true); 8 mArrayAdapter.clear(); 9 ClientConServer.getFriends(); 10 swipeLayout.setRefreshing(false); 11 } 12 }, 500); 13 14 } 15 });
在每次refresh的時候就從新獲取一下好友數據。
一個聊天軟件只要後臺就要從新啓動,不以爲很坑嘛?咱們就在friend活動添加一個服務開啓,即便咱們按了HOME鍵,也不會退出也能夠從新進入,並且最好在friend活動添加,這樣咱們成功的登錄了這樣也方便。
1 public class Myserver extends Service { 2 @Override 3 public IBinder onBind(Intent intent) { 4 return null; 5 } 6 7 @Override 8 public void onCreate(){ 9 super.onCreate(); 10 Notification notification=new Notification(R.mipmap.ic_launcher,"Just We running",System.currentTimeMillis()); 11 Intent intent=new Intent(this, friend.class); 12 PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0); 13 notification.setLatestEventInfo(this,"Just We","Online",pendingIntent); 14 Log.d("Myservice "," Oncreater"); 15 startForeground(1, notification); 16 } 17 18 }
1 Intent intentServer= new Intent(this, Myserver.class); 2 startService(intentServer);
而後在活動裏intent一下一邊開啓一個帶通知的前臺服務。並且你還能夠經過這個前臺的通知隨時進入friend活動界面。
咱們能登錄就要能註銷,這樣才便於咱們使用:
我把一個註銷賬號的方法寫進meau裏面了。
<item android:id="@+id/action_settings_base" android:title="@string/action_stop" android:showAsAction="never" tools:ignore="AppCompatResource" />
我寫了一個BaseActivity而後添加了這個meau讓後讓應該繼承的來繼承:
1 MainActivity.check_save.setChecked(false); 2 MainActivity.check_auto.setChecked(false); 3 MainActivity.editor.putString("USER_NAME", ""); 4 MainActivity.editor.putString("PASSWORD", ""); 5 Intent intent = new Intent(this, MainActivity.class); 6 startActivity(intent); 7 Intent stopintent = new Intent(this, Myserver.class); 8 stopService(stopintent); 9 user.UserName="";user.FromName_="";user.FromName="";user.UserName="";
清空checkbox,清空密碼,關閉服務,跳轉頁面清空全局變量的存儲。
1 if(content.equals("")) { 2 android.os.Message mm = new android.os.Message(); 3 mm.what=1; 4 mhandle.handleMessage(mm); 5 }
1 case 1: 2 Toast.makeText(useractivity.this, "不能發送空消息", Toast.LENGTH_SHORT).show(); 3 break;
功能是愈來愈豐富了呢!今天就改到這樣好了,求贊=-=