0

나는 중대한 문제에 직면 해있다. 내부에 service Wifi 연결을 열고 작업이 완료된 후 닫습니다. 어떤 점에서 service이 종료되기 때문에 연결이 열리고 열린 채로 남아있는 문제에 직면합니다.Android 서비스가 종료되면서 종료됩니다. 데이터 열기

START_STICKY을 사용하고 있거나 프로그래밍 방식으로 만 처리해야하는 방법이 있습니까?

EDIT : 두 대의 수신자 (BroadcastReceiver)간에 내 의도 정보를 공유 할 수 있습니까? 예를 들어, android.net.wifi.wifi_state_changed 조치에 대한 다른 수신자를 작성하고 기존 수신자는 android.intent.action.PHONE_STATE입니다. 달성 할 수 있다면 그것에 대해 뭔가 할 수 있습니다.

EDIT2 :

public class CallReceiver extends BroadcastReceiver { 
    private static final String LOG_TAG = "CallReceiver"; 
    private static final String CALL_ACTION = "android.intent.action.PHONE_STATE"; 

    @Override 
    public void onReceive(Context context, Intent callIntent) 
    { 
     Log.d(LOG_TAG, "----------------Inside onReceive of CallReceiver----------------"); 


     if (callIntent.getAction().equals(CALL_ACTION)) 
     { 

      try 
      { 
       Intent myIntent = new Intent(context, MyService.class); 
       context.startService(myIntent); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
       Log.d(LOG_TAG,"----------------Exception occured while starting service----------------"); 
      } 
     } 
    } 
} 


public class MyService extends Service { 
    private Context context; 
    private static final String LOG_TAG = "MyService"; 
    private Thread thread = null; 

    public MyService() 
    { 
     super(); 
     Log.d(LOG_TAG, "----------------Inside Email Service constructor----------------"); 
    } 

    public int onStartCommand(Intent myIntent, int flags, int startId) 
    { 
     Log.d(LOG_TAG, "----------------Email Service Command Started----------------"); 
     try 
     { 
      context = getApplicationContext(); 
      if(thread == null || !thread.isAlive()) 
      { 
       thread = new Thread(new MyRunnable("Email Sender", myIntent)); 
       thread.start(); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      Log.d(LOG_TAG, 
        "----------------Exception occured in Email Service onStartCommand----------------"); 
     } 
     return START_REDELIVER_INTENT; 
    } 

    class MyRunnable implements Runnable { 
     String name; 
     Intent myIntent; 

     public MyRunnable(String name, Intent myIntent) { 
      this.name = name; 
      this.myIntent = myIntent; 
     } 

     @Override 
     public void run() 
     { 
      try 
      { 
       doStuff(emailIntent); 
      } 
      catch (NumberFormatException e) 
      { 
       e.printStackTrace(); 
       Log.d(LOG_TAG, e.getMessage()); 
      } 
      catch (InterruptedException e) 
      { 
       e.printStackTrace(); 
       Log.d(LOG_TAG, e.getMessage()); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
       Log.d(LOG_TAG, e.getMessage()); 
      } 
      finally 
      { 
       stopSelf(); 
      } 
     } 
    } 

    private void doStuff(Intent emailIntent) throws InterruptedException, Exception 
    { 
     if (context != null) 
     { 
      boolean isWifiConnection = false; 

      try 
      { 
       // Check if WiFi connection is available ,if yes try opening it; 
       // Attempt to open WiFi connection 
       isWifiConnection = Utility.isEnableWifiSuccessful(getApplicationContext()); 
       Log.d(LOG_TAG, "----------------Wifi conn enabled = " + isWifiConnection 
           + "----------------"); 

       if (isWifiConnection) 
       { 
        // Do more stuff 
       } 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
       throw e; 
      } 
      finally 
      { 
       // Code never reaches here !! Somehow, the service stops and by 
       // the time the service stops, 
       // WiFi has been enabled 
       try 
       { 
        if (isWifiConnection) 
        { 
         Utility.isDisableWifiSuccessful(getApplicationContext()); 
        } 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
        Log.d(LOG_TAG, 
          "----------------Error occured while closing network connections----------------"); 
       } 
      } 
     } 
     else 
     { 
      Log.d(LOG_TAG, "----------------Context is null----------------"); 
     } 
    } 

    @Override 
    public IBinder onBind(Intent intent) 
    { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

을 이제 NetworkReceiver

public class NetworkReceiver extends BroadcastReceiver { 

    private static final String ACTION = "android.net.wifi.WIFI_STATE_CHANGED"; 
    private static final String LOG_TAG = "NetworkReceiver"; 

    @Override 
    public void onReceive(Context context, Intent networkIntent) 
    { 
     if(networkIntent.getAction().equals(ACTION)) 
     { 
      Log.d(LOG_TAG, "----------------Inside Network Receiver----------------"); 
      //Do something which will keep track who has opened the WiFi connection 
     } 
    } 
} 

다음 myIntentnetworkIntent주 정보와 MySerivce 해당 정보를 읽을 수 있습니다 나는 다른 수신기가있는 경우 다음과 같이 내 코드는 .

도움이된다면 정말 감사하겠습니다.

답변

0

메모리가 너무 부족한 경우 서비스가 이미 START_STICKY (을)를 사용 중이므로 서비스가 종료되면 메모리 리소스를 사용할 수있게되면 서비스가 다시 시작됩니다. 나는 연결이 열렸는지 확인하고 작업을 완료했는지 확인해야 할 수도 있습니다. 그런 다음 stopSelf()를 사용하여 서비스를 중지하십시오.

희망이 도움이됩니다.

덕분에, 라 메쉬

+0

서비스 종료는 연결을 열고 사람 (사용자/응용 프로그램)을 떠나는 경우입니다 발생할 수있는 문제를 닫 또는 그 나는 수프에서 오전 -versa. – Adithya