openfire+asmack搭建的安卓即时通讯(六) 15.4.16
啊啊啊啊啊啊啊啊,这东西越做越觉得是个深坑啊!
1.SharedPreferences.Editor的密码保存和自动登录:
首先还是从主界面开始,因为要提升一下用户体验自然要加入保存密码和自动登录的功能。
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" />
接着在主活动里写一下下:
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的按下和弹起的状态。
2.用户的注册、利用webview进行访问作者博客:
在下面加入了两个新的Button的用来注册和访问作者博客。
1)webview的使用:
加入了一个放一个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 }
2)注册的方法实现:
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用来清理所有的东西让用户重新入。
3.好友界面的修改:
find方法还没有弄完,所以先说之前的。
加入了谷歌的下拉刷新来及时的更新数据:
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的时候就重新获取一下好友数据。
4.添加服务防止后台清理:
一个聊天软件只要后台就要重新启动,不觉得很坑嘛?我们就在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活动界面。
5.注销帐号:
我们能登陆就要能注销,这样才便于我们使用:
我把一个注销帐号的方法写进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,清空密码,关闭服务,跳转页面清空全局变量的存储。
6.对发送进行限制:
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;
功能是越来越丰富了呢!今天就改到这样好了,求赞=-=