80,351
社区成员
发帖
与我相关
我的任务
分享
package com.android.book;
import com.android.book.DBAdapter;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Nearby extends SerialPortActivity {
TextView tv=null;
String bookname;
String str="";
String check, check2;
int find=0;
int i=1;
DBAdapter db = new DBAdapter(this);
@Override
protected void onDataReceived(final byte[] buffer, final int size) {
runOnUiThread(new Runnable() {
public void run() {
if ( tv!= null && size >0) {
str=str+new String(buffer,0,size);
int start=str.indexOf(8');
int end=str.indexOf('9');
while(end<start){
str=str.substring(start);
start=str.indexOf('8');
end=str.indexOf('9');
if(start<end-6){
check=str.substring(start+1,end);
if(find==0){
tv.append(check);
find=1;
find(check);
}
return;
}
if(end==-1)
break;
}
if(start<end-6){
check=str.substring(start+1,end);
if(find==0){
tv.append(check);
find=1;
find(check);
}
}
}
}
});
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int i = 4;
Intent intent = new Intent();
intent.setClass(Nearby.this,user9.class);
Bundle bundle = new Bundle();
bundle.putString("name",check);
bundle.putInt("i", i);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
private void find(String book)
{
int c=0;
db.open();
Cursor c1 = db.getISBN(book);
if (c1.moveToFirst())
{
c++;
//db.updateTitle(c1.getString(0), c1.getString(1), c1.getString(2), c1.getString(3), c1.getString(4), "yes");
DisplayTitle(c1,tv);
} while (c1.moveToNext());
db.close();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nearby);
Intent intent =this.getIntent();
tv=(TextView)findViewById(R.id.TextView_content);
/*button_return to user1*/
Button b1 = (Button) findViewById(R.id.Button_return);
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(Nearby.this,enter.class);
startActivity(intent);
Nearby.this.finish();
}
});
}
public void DisplayTitle (Cursor c,TextView tv)
{
tv.setText( c.getString(2) + "\n");
}
}
package com.android.book;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;
import com.android.book.Application;
import com.android.book.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.serialport.SerialPort;
public abstract class SerialPortActivity extends Activity {
protected Application mApplication;
protected SerialPort mSerialPort;
protected OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread;
private class ReadThread extends Thread {
@Override
public void run() {
super.run();
while(!isInterrupted()) {
int size;
try {
byte[] buffer = new byte[64];
if (mInputStream == null) return;
size = mInputStream.read(buffer);
if (size > 0) {
onDataReceived(buffer, size);
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
private void DisplayError(int resourceId) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Error");
b.setMessage(resourceId);
b.setPositiveButton("OK", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SerialPortActivity.this.finish();
}
});
b.show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApplication = (Application) getApplication();
try {
mSerialPort = mApplication.getSerialPort();
mOutputStream = mSerialPort.getOutputStream();
mInputStream = mSerialPort.getInputStream();
/* Create a receiving thread */
mReadThread = new ReadThread();
mReadThread.start();
} catch (SecurityException e) {
DisplayError(R.string.error_security);
} catch (IOException e) {
DisplayError(R.string.error_unknown);
} catch (InvalidParameterException e) {
DisplayError(R.string.error_configuration);
}
}
protected abstract void onDataReceived(final byte[] buffer, final int size);
@Override
protected void onDestroy() {
if (mReadThread != null)
mReadThread.interrupt();
mApplication.closeSerialPort();
mSerialPort = null;
super.onDestroy();
}
}
package com.android.book;
import com.android.book.DBAdapter;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Nearby extends SerialPortActivity {
private static final int Oxff = 0;
TextView tv=null;
String bookname;
String str="";
String str1="";
String check, check2;
int find=0;
int i=1;
DBAdapter db = new DBAdapter(this);
@Override
protected void onDataReceived(final byte[] buffer, final int size) {
runOnUiThread(new Runnable() {
public void run() {
if ( tv!= null && size >0) {
/****************************************************************************/
str1 = bytesToHexString(buffer);
int start = str1.indexOf('8');
int end = str1.indexOf('9');
while(end<start){
str1=str1.substring(start);
start=str1.indexOf('8');
end=str1.indexOf('9');
if(start<end-6){
check=str1.substring(start+1,end);
/*****************************************************************************/
if(find==0){
tv.append(check);
find=1;
find(check);
}
return;
}
if(end==-1)
break;
}
if(start<end-6){
check=str1.substring(start+1,end);
if(find==0){
tv.append(check);
find=1;
find(check);
}
}
}
}
});
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int i = 4;
Intent intent = new Intent();
intent.setClass(Nearby.this,user9.class);
Bundle bundle = new Bundle();
bundle.putString("name",check);
bundle.putInt("i", i);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
/************************** string to 16 *******************************************/
public static String bytesToHexString(byte[] src)
{
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length<= 0){
return null;
}
for(int i = 0;i < src.length;i++){
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if(hv.length() < 2){
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/*******************************************************************/
private void find(String book)
{
int c=0;
db.open();
Cursor c1 = db.getISBN(book);
if (c1.moveToFirst())
{
c++;
//db.updateTitle(c1.getString(0), c1.getString(1), c1.getString(2), c1.getString(3), c1.getString(4), "yes");
DisplayTitle(c1,tv);
} while (c1.moveToNext());
db.close();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nearby);
Intent intent =this.getIntent();
tv=(TextView)findViewById(R.id.TextView_content);
/*button_return to user1*/
Button b1 = (Button) findViewById(R.id.Button_return);
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(Nearby.this,enter.class);
startActivity(intent);
Nearby.this.finish();
}
});
}
public void DisplayTitle (Cursor c,TextView tv)
{
tv.setText( c.getString(2) + "\n");
}
}