Question

I'm using a USSDInterceptor service to get the USSD responce in my application. Here is the service:

    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;
    }   
    }

I dont know how to communicate with this service from my activity. I have used BroadcastReceiver in my activity like this:

    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));
}

But I couldn't get the broadcast. Am I doing something wrong? I dont know I should use BroadcastReceiver or I should communicate with my service by bindService. Whould you please help me in this problem?

Was it helpful?

Solution

I solved the problem by myself. The problem was in the place of calling 'unregisterReceiver'. I had called unregisterReceiver() on onPause() and it seemed that during getting USSD result, the BroadcastReceiver was unregistered. so I put it on onDestroy() method and finally I managed to get the broadcast message. Here is my onDestroy method:

 @Override
public void onDestroy() {
    Log.i(TAG, "unregister");
    unregisterReceiver(broadcastReceiver);      
    super.onDestroy();              
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top