2012-01-03 12 views
1

まず、現在の状況を説明します。 私は2つのサービスに2つの異なるスレッドを持っています(usbポートサービスから読み込み、Webリクエストサービスを行います)。私は応答が来るまで、私のGUIが停止要求を行う際に、シリアルサービスと間違っているが、RecordWebServiceでは何もありませんAndroidサービスのスレッド作成中のWebリクエストがブロックされています。

serialServiceIntent = new Intent(NDKSerialActivity.this, SerialService.class); 
startService(serialServiceIntent); 
webServiceIntent = new Intent(NDKSerialActivity.this, RecordWebService.class); 
startService(webServiceIntent); 

:私は次のように私の活動ののonCreateでそれらを始めています。また、私は睡眠一部を除去などのタイマーとタイマータスクで実行するスレッドを作成しようとした

public class RecordWebService extends Service 
{ 
public static final String SERVER_ADDRESS = "http://192.168.1.100:8080/MobilHM/rest"; 
private static final String TAG = RecordWebService.class.getSimpleName(); 
private RecordWebThread recordWebThread; 

@Override 
public void onStart(Intent intent, int startId) 
{ 
    super.onStart(intent, startId); 
    recordWebThread = new RecordWebThread(true); 
    recordWebThread.start(); 
} 

@Override 
public void onDestroy() 
{ 
    super.onDestroy(); 
    Log.i(TAG, "RecordWebService Destroyed"); 
} 

@Override 
public IBinder onBind(Intent intent) 
{ 
    return null; 
} 
} 

public class RecordWebThread extends Thread 
{ 
private static final String TAG = RecordWebThread.class.getSimpleName(); 
public boolean always; 


public RecordWebThread(boolean always) 
{ 
    this.always = always; 
} 

@Override 
public void run() 
{ 
    PatientRecord patientRecord = new PatientRecord(); 
    while (always) 
    { 
     RestClient restClient = new RestClient(RecordWebService.SERVER_ADDRESS + "/hello"); 
     try 
     { 
      restClient.execute(RequestMethod.GET); 
     } 
     catch (Exception e1) 
     { 
      Log.e(TAG, "", e1); 
     } 
     Log.i(TAG, "Server Response Code:->" + restClient.getResponseCode()); 
     Log.i(TAG, "Server Response:->" + restClient.getResponse()); 
     try 
     { 
      sleep(4 * 1000); 
     } 
     catch (InterruptedException e) 
     { 
      Log.e(TAG, "Web service interrupted", e); 
     } 
    } 
} 
} 

コードは、そのようなものです

public void sendRecord() 
{ 
    scanTask = new TimerTask() 
    { 
     public void run() 
     { 
      handler.post(new Runnable() 
      { 
       public void run() 
       { 
        RestClient restClient = new RestClient(RecordWebService.SERVER_ADDRESS + "/hello"); 
        try 
        { 
         restClient.execute(RequestMethod.GET); 
        } 
        catch (Exception e1) 
        { 
         Log.e(TAG, "", e1); 
        } 
        Log.i(TAG, "Server Response Code:->" + restClient.getResponseCode()); 
        Log.i(TAG, "Server Response:->" + restClient.getResponse()); 
       } 
      }); 
     } 
    }; 
    t.schedule(scanTask, 1000, 4000); 
} 

運がない場合、restClient.executeについては私のGUIがハングアップします。

あなたはどのように私は私の要求は私のGUIスレッドをブロックしないことができますhttp://www.giantflyingsaucer.com/blog/?p=1462

@ RestClient.javaを見つけることができますか?

編集:

public void sendRecord() 
{ 
    scanTask = new TimerTask() 
    { 
     public void run() 
     { 
      RestClient restClient = new RestClient(RecordWebService.SERVER_ADDRESS + "/hello"); 
      try 
      { 
       restClient.execute(RequestMethod.GET); 
      } 
      catch (Exception e1) 
      { 
       Log.e(TAG, "", e1); 
      } 
      Log.i(TAG, "Server Response Code:->" + restClient.getResponseCode()); 
      Log.i(TAG, "Server Response:->" + restClient.getResponse()); 
     } 
    }; 
    t.schedule(scanTask, 1000, 4000); 
} 

ハンドラがなければ、私は私の活動ののonCreateでこれを呼び出すが、まだUIぶら下がっ。

答えて

1

また、スレッドの問題を処理するIntentServiceを使用することもできます。

これは、例えばクラスです:

public class MyService extends IntentService { 

    public MyService() { 
     super("MyService"); 
    } 

    public MyService(String name) { 
     super(name);   
    } 

    @Override 
    protected void onHandleIntent(Intent arg0) { 

       //Do what you want 
     } 
} 

次に、あなただけの呼び出し:

Intent intent = new Intent(getApplicationContext(),MyService.class); 
startService(intent); 

編集:

あなたはこのような何かをしなければならない4秒ごとに同じことを繰り返すには:

PendingIntent serviceIntent= PendingIntent.getService(context, 
       0, new Intent(context, MyService.class), 0); 

    long firstTime = SystemClock.elapsedRealtime(); 
    AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 

    long intervalInSec = 4; 

    am.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, intervalInSec*1000, serviceIntent) 

;

+0

ありがとうございました。私の仕事は4秒間隔で繰り返されるべきです。 startServiceをタイマーなどで呼びますか? – GokcenG

+0

私は答えを編集しましたので、AlarmManagerを使用した例を見ることができます – sfratini

+0

ありがとうございました。私はちょうどあなたが言ったようにコードを編集しました。あなたのソリューションは貴重ですが、あなたはまだぶら下がっています。私はサービス、タイマーなどより.execute(要求)事柄に関連しているかもしれないと思う。私は非同期httpリクエストを探しているのと同じです。 – GokcenG

0

あなたのコード(2dバージョン)で次に起こること:スレッドを作成し、UIスレッドに何らかのネットインタラクションを要求します。リクエストを実行中にhandler.post(...)を除外してください。後で、これをリクエストの結果でUIを更新するための簡単な実行可能ファイルに使用できます。

+0

私が正しくあなたを理解している場合は、投稿の編集を確認することができます。そうでなければ私を修正してください。 – GokcenG

関連する問題