4 个解决方案
#1
系统中活跃程序运行时产生的log都会显示的。也包括系统自身的。
#2
哦,好,谢谢!问题其实是这样的:我想在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();
}
}
#3
顶一下下~~
#4
模拟器坏了吧,换一个试下
#1
系统中活跃程序运行时产生的log都会显示的。也包括系统自身的。
#2
哦,好,谢谢!问题其实是这样的:我想在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();
}
}
#3
顶一下下~~
#4
模拟器坏了吧,换一个试下