概述
上一篇文章是用线程来弄,但这样会很容易丢失数据,所以又研究了service,其实都是网上的例子,但那些例子都用button.onclicklistener方法获取数据,为了可以动态更新数据,我调试了很久。鉴于自己研究浪费太多时间,拿出来分享下。代码只有客户端。
1、login.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
/>
<EditText
android:id="@+id/username"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:hint="账号"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
/>
<EditText
android:id="@+id/password"
android:password="true"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:hint="密码"
/>
</TableRow>
</TableLayout>
<Button
android:id="@+id/login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:text="登录"
/>
</LinearLayout>
success.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/success"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不断接受信息,并提示"
/>
<EditText
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
/>
</LinearLayout>
2、类
LoginActivity03.java:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import com.login.MD5;
import com.login.R;
import com.util.ApplicationUtil;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity03 extends Activity{
private EditText usernameT;
private EditText passwordT;
private Button loginButton;
private Socket socket;
private DataOutputStream out;
private DataInputStream in;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
usernameT = (EditText) findViewById(R.id.username);
passwordT = (EditText) findViewById(R.id.password);
loginButton = (Button) findViewById(R.id.login);
loginButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
ApplicationUtil appUtil = (ApplicationUtil) LoginActivity03.this.getApplication();
try {
appUtil.init();
Socket socket = appUtil.getSocket();
out = appUtil.getOut();
in = appUtil.getIn();
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
String username = usernameT.getText().toString();
String password = passwordT.getText().toString();
MD5 md5 = new MD5();
String ss = md5.MD5(username+"|"+password);
try {
//发送数据
String str = "[login]|"+ss;
//String str = "[login]|"+md5.MD5(username)+"|"+md5.MD5(password);
out.writeBytes(str);
out.flush();
System.out.println("str ===> "+str);
//创建一个缓冲字节数
int r = in.available();
while(r==0){
r = in.available();
}
byte[] b = new byte[r];
in.read(b);
String result = new String(b,"utf-8");
System.out.println("result = "+result);
if(result.equals("登陆成功")){
Intent intent = new Intent();
intent.setClass(LoginActivity03.this, MainActivity03.class);
startActivity(intent);
}
else
{
Toast.makeText(LoginActivity03.this, "登录失败!!!", Toast.LENGTH_LONG).show();
System.out.println("登录失败!!!");
Intent intent = new Intent();
intent.setClass(LoginActivity03.this, MainActivity03.class);
startActivity(intent);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
MainActivity03.java:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import com.broadcast.UpdateService.UpdateData;
import com.image.TestActivity;
import com.login.R;
import com.util.ApplicationUtil;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore.Audio;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity03 extends Activity {
private EditText mess;
private Button kk;
private UpdateService.MyBinder serviceBinder;
private ServiceConnection serviceConn = new ServiceConnection(){
public void onServiceConnected(ComponentName arg0, IBinder service) {
serviceBinder = (UpdateService.MyBinder)service;
if(serviceBinder!=null)
{
serviceBinder.setDate(mess, new UpdateData() {
public void update(EditText mess, String data) {
mess.append(data+"n");
}
});
}
}
public void onServiceDisconnected(ComponentName arg0) {
serviceBinder = null;
}};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
mess = (EditText) findViewById(R.id.message);
Intent intent = new Intent(MainActivity03.this,UpdateService.class);
this.bindService(new Intent("com.broadcast.UpdateService"),this.serviceConn,BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
this.unbindService(serviceConn);
super.onDestroy(); //注意先后
}
}
UpdateService.java:
import java.io.DataInputStream; import java.io.IOException; import com.login.MainActivity; import com.util.ApplicationUtil; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.util.Log; import android.widget.EditText; public class UpdateService extends Service { private String data; private Handler handler; public boolean isRunning; private DataInputStream in; @Override public IBinder onBind(Intent intent) { return new MyBinder(); } @Override public void onCreate() { super.onCreate(); ApplicationUtil appUtil = (ApplicationUtil) UpdateService.this.getApplication(); in = appUtil.getIn(); } public class MyBinder extends Binder { public void setDate(final EditText tv, final UpdateData updata) { new Thread(new oneThread()).start(); handler = new Handler() { public void handleMessage(Message msg) { updata.update(tv,data); } }; } } public interface UpdateData { public void update(EditText tv, String data); } public class oneThread implements Runnable{ public boolean isRunning = true; public void run() { while(isRunning){ try { Thread.sleep(500); //创建一个缓冲字节数 int r = in.available(); while(r==0){ r = in.available(); } byte[] b = new byte[r]; in.read(b); data = new String(b,"utf-8"); //每当读到来自服务器的数据后,发送消息通知程序页面显示数据 Message msg = handler.obtainMessage(); msg.obj = data; handler.sendMessage(msg); Log.v("thread", data); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
ApplicationUtil.java:
测试效果的图片,我也想黏贴上去,不知道怎弄。流程就是登陆成功后,跳转到MainActivity03的界面,这个界面会不断从输入流读出数据到控件中。import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import android.app.Application; public class ApplicationUtil extends Application{ private Socket socket; private DataOutputStream out = null; private DataInputStream in = null; public ApplicationUtil() throws IOException { super(); } public void init() throws IOException, Exception{ this.socket = new Socket("192.168.1.104",10202); this.out = new DataOutputStream(socket.getOutputStream()); this.in = new DataInputStream(socket.getInputStream()); } public Socket getSocket() { return socket; } public void setSocket(Socket socket) { this.socket = socket; } public DataOutputStream getOut() { return out; } public void setOut(DataOutputStream out) { this.out = out; } public DataInputStream getIn() { return in; } public void setIn(DataInputStream in) { this.in = in; } }
左边是服务端的数据窗口,
最后
以上就是无私黄豆为你收集整理的android客户端用bindservice来动态更新UI数据的实例的全部内容,希望文章能够帮你解决android客户端用bindservice来动态更新UI数据的实例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复