0

As we know that, when we use any menu in sim toolkit it send command to mobile network in ussd or sms format. I need that sms or ussd to record and show it to me in android application.

I am calling the library service which i got in main activity like this

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent srvIntent = new Intent(this, CDUSSDService.class);
    startService(srvIntent);


}   
}

USSD Service Class is like that:

    public class CDUSSDService extends Service{

    private String TAG = CDUSSDService.class.getSimpleName();
    private boolean mActive = true;//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");

                showtoast(""+"activate ussd listener");
            }
            else if(intent.getAction().equals(Intent.ACTION_DELETE))
            {
                mActive = false;
                Log.d(TAG, "deactivate ussd listener");
                showtoast(""+"DeActivate ussd listener");
            }
        }
    };

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

        public void setMmiString(String number) throws RemoteException 
        {
            Log.d (TAG, "setMmiString:" + number);
            showtoast("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);
            showtoast("GET Usr Message:"+text);

            if(mActive == false){
                //listener is still inactive, so return whatever we got
                Log.d(TAG, "inactive " + text);
                showtoast("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();

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


    public void showtoast(String str)
    {
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
    }
    }

I have created MainActivity inside the UssdLibray structure is like that in the below picture:

enter image description here

Krishna Kachhela
  • 773
  • 1
  • 7
  • 24
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
  • i have started this service and i am not getting any SHOWTOAST() message when i send or receive message,I want to show the USSD/SMS in textview in my main activity please i am really stuck in this – Pir Fahim Shah Feb 01 '16 at 08:03

1 Answers1

2

you can do this with the help of this library https://github.com/alaasalman/ussdinterceptor