1

サービスなしで使用すると正常に動作する電話Webサーバーを作成するAndroidアプリを開発していますが、Webサーバーがバックグラウンドで実行されるようにサービスを使用したいこれは私のコードです.

サービス ランナー クラス:

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
4

1 に答える 1

0

問題は解決しました。アクションを追加しましintent filterたが、正常に動作しています。

コードは次のとおりです。

<service android:name="dolphin.developers.com.webser" android:enabled="true">
    <intent-filter>
        <action android:name="dolphin.developers.com.webserv"></action>
    </intent-filter>
</service>
于 2013-08-12T16:43:02.847 に答える