当前位置: 编程技术>移动开发
本页文章导读:
▪利用wifi在同一个局域网下兑现两部手机之间的通讯 利用wifi在同一个局域网下实现两部手机之间的通讯
需要用到的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
&l.........
▪ 记住密码和自动登录的设置 记住密码跟自动登录的设置
自动登录跟记住密码
首先定义一个登陆页面的ActivityLoginActivity
写道
package cn.hwttnet.com.ui; import android.app.Activity; import android.content.Intent; import android.content.Shar.........
▪ Log里头的几个方法 Log里面的几个方法
Log信息会在LogCat中显示。
方法名
日志信息类型
显示颜色
1
Log.v()
VERBOSE
黑色
2
Log.d()
DEBUG
蓝色
3
Log.i()
INFO
绿色
4
Log.w()
WARN
橙色
5
Log.e()
ERROR
红色
.........
[1]利用wifi在同一个局域网下兑现两部手机之间的通讯
来源: 互联网 发布时间: 2014-02-18
利用wifi在同一个局域网下实现两部手机之间的通讯
需要用到的权限
service端
client端
需要用到的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
service端
public class MainActivity extends Activity {
private Button start = null;
private EditText bufferText = null;
private Button send = null;
private ServerThread serverThread = null;
private String sendBuffer = null;
private String receiveBuffer = null;
private TcpSocketServer tss = null;
private TextView receiveView = null;
private Handler handler = new Handler(){//线程与UI交互更新界面
public void handleMessage(Message msg){
receiveView.setText(receiveBuffer);
Toast.makeText(MainActivity.this, receiveBuffer, Toast.LENGTH_SHORT).show();
}
};
private String intToIp(int i) {
return (i & 0xFF ) + "." +
((i >> 8 ) & 0xFF) + "." +
((i >> 16 ) & 0xFF) + "." +
( i >> 24 & 0xFF) ;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取wifi服务
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
//判断wifi是否开启
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = intToIp(ipAddress);
TextView tv=(TextView) findViewById(R.id.ip);
tv.setText("本机IP:"+ip);
receiveView = (TextView)this.findViewById(R.id.receiveID);
start = (Button) this.findViewById(R.id.startID);
//监听服务器开启
start.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(serverThread == null){
EditText portEditText = (EditText)MainActivity.this.findViewById(R.id.portID);
String port = portEditText.getText().toString().trim();
serverThread = new ServerThread(port);
serverThread .start();
Toast.makeText(MainActivity.this, port, Toast.LENGTH_SHORT).show();
}
}});
send = (Button)this.findViewById(R.id.sendID);
bufferText = (EditText)this.findViewById(R.id.bufferID);
//监听发送信息
send.setOnClickListener(new OnClickListener() {
public void onClick(View v){
sendBuffer = bufferText.getText().toString().trim();
if(sendBuffer != null)//为了避免线程把它弄为buffer = null;
Toast.makeText(MainActivity.this, sendBuffer, Toast.LENGTH_SHORT).show();
}
});
}
class ServerThread extends Thread{
private int port;
public ServerThread (String port){
this.port = Integer.parseInt(port);
}
public void run(){
//建立服务端
if(tss == null)
tss = new TcpSocketServer(this.port);
new Thread(new WriteThread()).start();//开启“写”线程
new Thread(new ReadThread()).start();//开启“读”线程
}
private class ReadThread implements Runnable{
public void run(){
while(true){
if((receiveBuffer = tss.getMessage()) != null){//收到不为null的信息就发送出去
handler.sendEmptyMessage(0);
}
}
}
}
private class WriteThread implements Runnable{
public void run(){
while(true){
try {
//发送数据
if(sendBuffer != null){
//tss.sendMessage(1821,buffer);
tss.sendMessage(sendBuffer);
sendBuffer = null;//清空,不让它连续发
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}public class TcpSocketServer {
private ServerSocket ss =null;
private Socket s = null;
private OutputStream out = null;
private InputStream in = null;
private String receiveBuffer = null;
public TcpSocketServer(int port){
//新建ServerSocket对象,端口为传进来的port;
try {
//ss= new ServerSocket(1821);
System.out.print("no");
ss = new ServerSocket(port);
System.out.print("yes");
s = ss.accept();
out = s.getOutputStream();
in = s.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String buffer)throws Exception{
//新建Socket通信对象,接受客户端发来的请求accept();
//Socket s = ss.accept();
//创建输入流对象InputStream
InputStream bais = new ByteArrayInputStream(buffer.getBytes());
byte[] buff = new byte[1024];
bais.read(buff);
out.write(buff);
out.flush();
}
public String getMessage(){
byte[] temp = new byte[1024];
try{
if(in.read(temp) > 0)
{
return receiveBuffer = new String(temp).trim();
}} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String receiveMessage(){
return null;
}
}client端
public class MainActivity extends Activity {
private EditText ipEdit = null;
private EditText portEdit = null;
private EditText buffEdit = null;
private Button startButton = null;
private Button sendButton = null;
private TextView receiveView = null;
private Socket s = null;
private byte[] receiveBuffer = new byte[1024];
private String sendBuffer = new String();
private String ip = null;
private int port ;
private int cmdCount = 0;
private Handler handler = new Handler(){//线程与UI交互更新界面
public void handleMessage(Message msg){
receiveView.setText(new String(receiveBuffer).trim());
Arrays.fill(receiveBuffer, (byte)0);//清空
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.init();
/*开启socket通信*/
startButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(s == null){//这里要设置验证!!!!!!!!!
/*设定ip和port*/
ip = ipEdit.getText().toString();
port = Integer.parseInt(portEdit.getText().toString());
/*开启socket线程*/
new Thread(new SocketClientControl(ip,port)).start();
}
Toast.makeText(MainActivity.this, "服务器连接成功", Toast.LENGTH_SHORT).show();
}
});
/*发送数据*/
sendButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(s != null)
sendBuffer = buffEdit.getText().toString();
Toast.makeText(MainActivity.this, "send -> "+sendBuffer, Toast.LENGTH_SHORT).show();
}
});
}
private void init(){
startButton = (Button) this.findViewById(R.id.startID);
sendButton = (Button) this.findViewById(R.id.sendID);
ipEdit = (EditText) this.findViewById(R.id.ipID);
portEdit = (EditText) this.findViewById(R.id.portID);
buffEdit = (EditText) this.findViewById(R.id.buffID);
receiveView = (TextView) this.findViewById(R.id.recieiveID);
}
private class SocketClientControl implements Runnable{
private InputStream in = null;
private OutputStream out = null;
public SocketClientControl(){
}
public SocketClientControl(String ip,int port){
try {
s = new Socket(ip,port);//获得链接
in = s.getInputStream();//获得输入流
out = s.getOutputStream();//获得输出流
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);//要是出问题,就线程退出
}
}
public void run(){
new Thread(new WriteThread()).start();//开启“写”线程
new Thread(new ReadThread()).start();//开启“读”线程
}
private class ReadThread implements Runnable{
public void run() {
while(true){
try {
if(in.read(receiveBuffer) > 0){//等待命令的输入
cmdCount ++; ;
handler.sendEmptyMessage(0);//发送信息,更新UI
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private class WriteThread implements Runnable{
public void run(){
while(true){
if(!sendBuffer.equals("")){
try {
out.write(sendBuffer.getBytes());//输出
out.flush();//输出刷新缓冲
} catch (IOException e) {
e.printStackTrace();
}
sendBuffer = "";
}
}
}
}
}
}public class SocketClientSingle {
private static Socket s = null;
private SocketClientSingle()
{
}
public static Socket getSocket(String ip,int port){
try {
s = new Socket(ip,port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
[2] 记住密码和自动登录的设置
来源: 互联网 发布时间: 2014-02-18
记住密码跟自动登录的设置
自动登录跟记住密码
首先定义一个登陆页面的ActivityLoginActivity
写道
package cn.hwttnet.com.ui;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class LoginActivity extends Activity {
private EditText username, pass, url;
CheckBox remberPass, autoLogin;
Button login;
SharedPreferences sp;
Editor ed;
ImageButton ins;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username = (EditText) findViewById(R.id.et_username);
pass = (EditText) findViewById(R.id.et_password);
url = (EditText) findViewById(R.id.et_url);
remberPass = (CheckBox) findViewById(R.id.remeber);
autoLogin = (CheckBox) findViewById(R.id.autoLogin);
login = (Button) findViewById(R.id.login);
ins = (ImageButton) findViewById(R.id.shuoming);
ins.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent3 = new Intent(LoginActivity.this,
InstrutionActivity.class);
startActivity(intent3);
}
});
sp = getSharedPreferences("users", MODE_WORLD_READABLE);
ed = sp.edit();
// 从SharedPreferences里边取出 记住密码的状态
if (sp.getBoolean("ISCHECK", false)) {
// 将记住密码设置为被点击状态
remberPass.setChecked(true);
// 然后将值赋值给EditText
username.setText(sp.getString("oa_name", ""));
pass.setText(sp.getString("oa_pass", ""));
url.setText(sp.getString("oa_url", ""));
// 获取自动登录按钮的状态
if (sp.getBoolean("AUTO_ISCHECK", false)) {
// 设置自动登录被点击 然后实现跳转
autoLogin.setChecked(true);
Intent intent1 = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent1);
}
}
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LoginMain();
}
});
// 将点击的checkBOx存入到users中
remberPass.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Boolean isChecked1 = remberPass.isChecked();
ed.putBoolean("ISCHECK", isChecked1);
ed.commit();
}
});
// 设置自动登录默认为不点击
Boolean value1 = sp.getBoolean("AUTO_ISCHECK", false);
autoLogin.setChecked(value1);
autoLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Boolean isChecked2 = autoLogin.isChecked();
ed.putBoolean("AUTO_ISCHECK", isChecked2);
ed.commit();
}
});
// 如果记住密码跟自动登录都被选中就选择登录跳转
if (remberPass.isChecked() && autoLogin.isChecked()) {
LoginMain();
}
}
protected void LoginMain() {
// TODO Auto-generated method stub
// 将信息存入到users里面
ed.putString("oa_name", username.getText().toString());
ed.putString("oa_pass", pass.getText().toString());
ed.putString("oa_url", url.getText().toString());
ed.commit();
if (TextUtils.isEmpty(username.getText().toString())) {
Toast.makeText(this, "请输入用户名", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(pass.getText().toString())) {
Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(url.getText().toString())) {
Toast.makeText(this, "请输入连接地址", Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
}
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class LoginActivity extends Activity {
private EditText username, pass, url;
CheckBox remberPass, autoLogin;
Button login;
SharedPreferences sp;
Editor ed;
ImageButton ins;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username = (EditText) findViewById(R.id.et_username);
pass = (EditText) findViewById(R.id.et_password);
url = (EditText) findViewById(R.id.et_url);
remberPass = (CheckBox) findViewById(R.id.remeber);
autoLogin = (CheckBox) findViewById(R.id.autoLogin);
login = (Button) findViewById(R.id.login);
ins = (ImageButton) findViewById(R.id.shuoming);
ins.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent3 = new Intent(LoginActivity.this,
InstrutionActivity.class);
startActivity(intent3);
}
});
sp = getSharedPreferences("users", MODE_WORLD_READABLE);
ed = sp.edit();
// 从SharedPreferences里边取出 记住密码的状态
if (sp.getBoolean("ISCHECK", false)) {
// 将记住密码设置为被点击状态
remberPass.setChecked(true);
// 然后将值赋值给EditText
username.setText(sp.getString("oa_name", ""));
pass.setText(sp.getString("oa_pass", ""));
url.setText(sp.getString("oa_url", ""));
// 获取自动登录按钮的状态
if (sp.getBoolean("AUTO_ISCHECK", false)) {
// 设置自动登录被点击 然后实现跳转
autoLogin.setChecked(true);
Intent intent1 = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent1);
}
}
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LoginMain();
}
});
// 将点击的checkBOx存入到users中
remberPass.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Boolean isChecked1 = remberPass.isChecked();
ed.putBoolean("ISCHECK", isChecked1);
ed.commit();
}
});
// 设置自动登录默认为不点击
Boolean value1 = sp.getBoolean("AUTO_ISCHECK", false);
autoLogin.setChecked(value1);
autoLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Boolean isChecked2 = autoLogin.isChecked();
ed.putBoolean("AUTO_ISCHECK", isChecked2);
ed.commit();
}
});
// 如果记住密码跟自动登录都被选中就选择登录跳转
if (remberPass.isChecked() && autoLogin.isChecked()) {
LoginMain();
}
}
protected void LoginMain() {
// TODO Auto-generated method stub
// 将信息存入到users里面
ed.putString("oa_name", username.getText().toString());
ed.putString("oa_pass", pass.getText().toString());
ed.putString("oa_url", url.getText().toString());
ed.commit();
if (TextUtils.isEmpty(username.getText().toString())) {
Toast.makeText(this, "请输入用户名", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(pass.getText().toString())) {
Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(url.getText().toString())) {
Toast.makeText(this, "请输入连接地址", Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
}
定义一个登录界面的XML login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearlayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageButton
android:id="@+id/shuoming"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dip"
android:layout_marginTop="40dip"
android:src="/blog_article/@drawable/shuoming/index.html" />
<ImageView
android:id="@+id/image_logo"
android:layout_width="250dip"
android:layout_height="150dip"
android:paddingLeft="8dip"
android:paddingRight="8dip"
android:src="/blog_article/@drawable/logo/index.html" />
<TableLayout
android:id="@+id/table01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:collapseColumns="4"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tableow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:paddingLeft="10dip"
android:textColor="#000000"
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名" />
<EditText
android:id="@+id/et_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:paddingLeft="10dip"
android:textColor="#000000"
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:textColor="#000000"
android:id="@+id/tableow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:paddingLeft="10dip"
android:textColor="#000000"
android:id="@+id/tv_url"
android:layout_height="wrap_content"
android:text="地址" />
<EditText
android:id="@+id/et_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/tableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TableRow
android:id="@+id/tableow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:textColor="#000000"
android:id="@+id/remeber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dip"
android:text="记住密码" />
<CheckBox
android:textColor="#000000"
android:id="@+id/autoLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录" />
</TableRow>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="180dip"
android:layout_height="wrap_content"
android:layout_marginLeft="80dip"
android:text="登录" />
</LinearLayout>
以上就实现了登录得记住密码,跟自动登录的实现
[3] Log里头的几个方法
来源: 互联网 发布时间: 2014-02-18
Log里面的几个方法
Log信息会在LogCat中显示。
方法名 日志信息类型 显示颜色 1 Log.v() VERBOSE 黑色 2 Log.d() DEBUG 蓝色 3 Log.i() INFO 绿色 4 Log.w() WARN 橙色 5 Log.e() ERROR 红色
最新技术文章: