2014-01-29 2 views
0

USSDInterceptor 서비스를 사용하여 응용 프로그램에서 USSD 응답을 받고 있습니다. 서비스는 다음과 같습니다.서비스와 통신하는 방법

package com.codedemigod.services; 

    import com.android.internal.telephony.IExtendedNetworkService; 


    public class CDUSSDService extends Service{ 

    private String TAG = CDUSSDService.class.getSimpleName(); 
    private boolean mActive = false; //we will only activate this "USSD listener" when we want it 

    BroadcastReceiver receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      if(intent.getAction().equals(Intent.ACTION_INSERT)){ 
       //activity wishes to listen to USSD returns, so activate this 
       mActive = true; 
       Log.d(TAG, "activate ussd listener"); 
      } 
      else if(intent.getAction().equals(Intent.ACTION_DELETE)){ 
       mActive = false; 
       Log.d(TAG, "deactivate ussd listener"); 
      } 
     } 
    }; 

    private final IExtendedNetworkService.Stub mBinder = new IExtendedNetworkService.Stub() { 
     public void clearMmiString() throws RemoteException { 
      Log.d(TAG, "called clear"); 
     } 

     public void setMmiString(String number) throws RemoteException { 
      Log.d (TAG, "setMmiString:" + number); 
     } 

     public CharSequence getMmiRunningText() throws RemoteException { 
      if(mActive == true){ 
       return null; 
      } 

      return "USSD Running"; 
     } 

     public CharSequence getUserMessage(CharSequence text) 
       throws RemoteException { 
      Log.d(TAG, "get user message " + text); 
      //getApplicationContext().sendBroadcast(new Intent("ussdMsg")); 


      if(mActive == false){ 
       //listener is still inactive, so return whatever we got 
       Log.d(TAG, "inactive " + text); 
       return text; 
      } 

      //listener is active, so broadcast data and suppress it from default behavior 

      //build data to send with intent for activity, format URI as per RFC 2396 
      Uri ussdDataUri = new Uri.Builder() 
      .scheme(getBaseContext().getString(R.string.uri_scheme)) 
      .authority(getBaseContext().getString(R.string.uri_authority)) 
      .path(getBaseContext().getString(R.string.uri_path)) 
      .appendQueryParameter(getBaseContext().getString(R.string.uri_param_name), text.toString()) 
      .build(); 


      getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_GET_CONTENT, ussdDataUri)); 

      mActive = false; 
      return null; 
     } 
    }; 



    @Override 
    public IBinder onBind(Intent intent) { 
     Log.i(TAG, "called onbind"); 

     //the insert/delete intents will be fired by activity to activate/deactivate listener since service cannot be stopped 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(Intent.ACTION_INSERT); 
     filter.addAction(Intent.ACTION_DELETE); 
     filter.addDataScheme(getBaseContext().getString(R.string.uri_scheme)); 
     filter.addDataAuthority(getBaseContext().getString(R.string.uri_authority), null); 
     filter.addDataPath(getBaseContext().getString(R.string.uri_path), PatternMatcher.PATTERN_LITERAL); 
     registerReceiver(receiver, filter); 

     return mBinder; 
    } 
    } 

본인의 활동에서이 서비스와 통신하는 방법을 모릅니다. BroadcastReceiver를 다음과 같이 사용했습니다 :

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     updateUI(intent); 
    } 
}; 

@Override 
public void onResume() { 
    super.onResume(); 
    registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_GET_CONTENT)); 
} 

그러나 브로드 캐스트를받을 수 없습니다. 내가 뭔가 잘못하고 있는거야? 나는 BroadcastReceiver를 사용해야한다는 것을 모른다. 그렇지 않으면 bindService가 내 서비스와 통신해야한다. 이 문제를 도와 주시겠습니까?

+0

나는 당신의 코드를 시도합니다. 하지만 오류가 발생하면 com.android.internal.telephony.IExtendedNetworkService를 해결할 수 없습니다. – Plugie

답변

0

저는 혼자서 문제를 해결했습니다. 문제는 'unregisterReceiver'라고하는 자리에있었습니다. onPause()에서 unregisterReceiver()를 호출했으며 USSD 결과를 얻는 동안 BroadcastReceiver가 등록 취소 된 것처럼 보였습니다. 그래서 onDestroy() 메서드에 넣고 마지막으로 브로드 캐스트 메시지를 가져 왔습니다. 여기에 내 onDestroy 방법이 있습니다 :

@Override 
public void onDestroy() { 
    Log.i(TAG, "unregister"); 
    unregisterReceiver(broadcastReceiver);  
    super.onDestroy();    
} 
관련 문제