eclipse logcat一直无限期打印输出 系统中活跃程序运行时产生的log都会显示的。也包括系统自身的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 哦,好,谢谢!问题其实是这样的:我想在activity中开启一个服务,然后在服务中开启一个线程来进行socket通信,但是public class MainActivity extends Activity { private Button btnStart; private Button btnStop; private Button btnShowGPS; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStart = (Button) this.findViewById(R.id.btnStart); btnStop = (Button) this.findViewById(R.id.btnStop); btnStart.setOnClickListener(new onStartServiceClickListener()); btnStop.setOnClickListener(new onStopServiceClickListener()); } class onStartServiceClickListener implements OnClickListener { private String TAG = "V"; public void onClick(View v) Intent intent=new Intent(); intent.setClass(MainActivity.this,SendGPSService.class); Bundle bundle = new Bundle(); bundle.putCharSequence("IP", "10.13.33.136"); bundle.putInt("Port", 5000); intent.putExtras(bundle); startService(intent); } } class onStopServiceClickListener implements OnClickListener { public void onClick(View v) { Intent intent=new Intent(); intent.setClass(MainActivity.this,SendGPSService.class); stopService(intent); } }}package com.example.communication;import java.io.BufferedWriter;import java.io.IOException;import java.io.OutputStreamWriter;import java.net.Socket;import java.net.UnknownHostException;import android.app.Service;import android.content.Intent;import android.os.Bundle;import android.os.IBinder;import android.util.Log;public class SendGPSService extends Service{ private ClientThread clientThread = null; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } public void onCreate() { super.onCreate(); Intent intent = new Intent(); Bundle bundle = intent.getExtras(); clientThread = new ClientThread(bundle.getString("IP"), bundle.getInt("Port")); clientThread.start(); } public int onStartCommond(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } public void onDestroy() { super.onDestroy(); if(clientThread.isAlive()) clientThread.interrupt(); clientThread = null; }}package com.example.communication;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.Socket;import android.util.Log;public class ClientThread{ String IP = null; int Port; Runnable runnable = null; ClientThread(final String IP, final int Port) { this.IP = IP; this.Port = Port; this.runnable = new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Socket socket = new Socket(IP, Port); sendMsg(socket, "hello world!"); } catch (IOException e) { e.printStackTrace(); } } }; Thread thread = new Thread(this.runnable); thread.start(); } private void sendMsg(Socket socket, String msg) throws IOException { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); writer.write(msg.replace("\n", " ") + "\n"); writer.flush(); writer.close(); }}代码是这样的,但是线程似乎不能启动,求解~ eclipse打开android工程过慢的问题 在Android编程中将jpg图片转换成bitmap之后像素大小不一样 语音搜索搜索到exit,点击后报错,跪求高手解决! BroadcastReceiver和intent 寻找问题 Android 监控短信发送 指定名字的通话记录查询 android 蓝牙连接状态监测-BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED 朋友们 帮帮 小弟 垂直自动滚动问题(第一行固定,其他行自动滚动)有人知道怎么实现吗? 招聘android高级开发-苏州昆山 android,view的重绘 关于ViewPager
private Button btnStart;
private Button btnStop;
private Button btnShowGPS;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnStart = (Button) this.findViewById(R.id.btnStart);
btnStop = (Button) this.findViewById(R.id.btnStop); btnStart.setOnClickListener(new onStartServiceClickListener());
btnStop.setOnClickListener(new onStopServiceClickListener());
} class onStartServiceClickListener implements OnClickListener
{
private String TAG = "V"; public void onClick(View v)
Intent intent=new Intent();
intent.setClass(MainActivity.this,SendGPSService.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("IP", "10.13.33.136");
bundle.putInt("Port", 5000);
intent.putExtras(bundle);
startService(intent);
}
}
class onStopServiceClickListener implements OnClickListener
{
public void onClick(View v)
{
Intent intent=new Intent();
intent.setClass(MainActivity.this,SendGPSService.class);
stopService(intent);
}
}
}
package com.example.communication;import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;public class SendGPSService extends Service
{
private ClientThread clientThread = null;
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
public void onCreate()
{
super.onCreate();
Intent intent = new Intent();
Bundle bundle = intent.getExtras();
clientThread = new ClientThread(bundle.getString("IP"), bundle.getInt("Port"));
clientThread.start();
}
public int onStartCommond(Intent intent, int flags, int startId)
{
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy()
{
super.onDestroy();
if(clientThread.isAlive())
clientThread.interrupt();
clientThread = null;
}
}
package com.example.communication;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;import android.util.Log;public class ClientThread{ String IP = null;
int Port;
Runnable runnable = null;
ClientThread(final String IP, final int Port)
{
this.IP = IP;
this.Port = Port;
this.runnable = new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
try {
Socket socket = new Socket(IP, Port);
sendMsg(socket, "hello world!");
} catch (IOException e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread(this.runnable);
thread.start();
} private void sendMsg(Socket socket, String msg) throws IOException {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write(msg.replace("\n", " ") + "\n");
writer.flush();
writer.close();
}
}代码是这样的,但是线程似乎不能启动,求解~