我正在开发一个 android 应用程序,它可以制作一个电话网络服务器,当我在没有服务的情况下使用它时它可以正常工作,但我想使用服务,以便我的网络服务器在后台运行这是我的代码,请看一下..
服务跑者类:
package dolphin.developers.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import dolphin.devlopers.com.R;
public class web extends Activity implements OnClickListener {
public static final int progress_bar_type1 = 0;
private static final String TAG = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.hotmail);
Button btnShowProgressa;
Button buttonStop = (Button) findViewById(R.id.button1ad);
buttonStop.setOnClickListener(this);
btnShowProgressa = (Button) findViewById(R.id.button2);
btnShowProgressa.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button2:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, webser.class));
break;
case R.id.button1ad:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, webser.class));
break;
}
}
}
网络服务器服务:
package dolphin.developers.com;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class webser extends Service {
Thread hi;
private static final String TAG = null;
JHTTP pro;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
hi= new Thread(){
public void run(){
try{
File documentRootDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/");
pro = new JHTTP(documentRootDirectory, 1234);
}
catch(IOException e ){
e.printStackTrace();
}
}
}; hi.start();
}
@SuppressWarnings("deprecation")
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
readysteadypo.stop();
pro.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
pro.start();
}
}
清单文件:
<Service
android:name="dolphin.developers.com.webser"/>
日志猫:
08-05 13:04:12.221: W/ActivityManager(73): Unable to start service Intent { cmp=dolphin.devlopers.com/dolphin.developers.com.hot1 }: not found