Skip to content

Instantly share code, notes, and snippets.

@victorkifer
Created January 25, 2014 13:19
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save victorkifer/8616266 to your computer and use it in GitHub Desktop.
Save victorkifer/8616266 to your computer and use it in GitHub Desktop.
USSDNetworkService used to replace Android default one and get USSD messages results.
package com.username.ussd;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.util.Log;
import com.android.internal.telephony.IExtendedNetworkService;
import com.username.ussd.R; // your package name
/**
* Service implements IExtendedNetworkService interface.
* Service must have name "com.android.ussd.IExtendedNetworkService" of the intent declared
* in the Android manifest file so com.android.phone.PhoneUtils class bind
* to this service after system rebooted.
* Please note service is loaded after system reboot!
*/
public class USSDNetworkService extends Service {
public static final String TAG = "USSDNetworkService";
public static CharSequence mRetVal = null;
private static boolean mActive = true; // if active, hide messages
private boolean change = false; // if mActive was changed while ussd was running, still hide result of this ussd
private String msgUssdRunning = null;
public static void setActive(boolean active) {
mActive = active;
}
private final IExtendedNetworkService.Stub mBinder = new IExtendedNetworkService.Stub() {
@Override
public void setMmiString(String number) throws RemoteException {
Log.d(TAG, "setMmiString: " + number);
change = !mActive;
}
@Override
public CharSequence getMmiRunningText() throws RemoteException {
return msgUssdRunning;
}
@Override
public CharSequence getUserMessage(CharSequence text)
throws RemoteException {
Log.d(TAG,text.toString());
if(!mActive||change) {
mRetVal = text;
change = false;
return null;
}
else return text;
}
@Override
public void clearMmiString() throws RemoteException {
Log.d(TAG,"clearMmiString");
}
};
/**
* Put stamp to the App SharedPreferences when PhoneUtils bind to the service
* after Android has rebooted. Without reboot phone application does not bind to this service!
*/
@Override
public IBinder onBind(Intent intent) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(TAG, true);
editor.commit();
msgUssdRunning = getString(R.string.ussd_running_text);
return mBinder;
}
public IBinder asBinder() {
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
}
@victorkifer
Copy link
Author

This probably won't work if another application already use it. Also it my not work on some versions of Android. I've tested it on Android 2.1-2.3, 4.1. Also I've founded out that it not work on some sony mobile phone(don't remember model), so I think it may not work on some devices according to modified firmwares.

@nhducit
Copy link

nhducit commented Feb 26, 2014

I also do many research about USSD, but i can't get the response and how to close ussd result dialog, like this application done: https://play.google.com/store/apps/details?id=com.iba.ussdchecker&hl=en.

could you give me an real example, please

@juancresc
Copy link

What about manifest? How can I use this service?

@swistaczek
Copy link

How about reading USSD response in 4.2.2?

@aeroditya21
Copy link

Does this work in latest android versions like 4.0+ ?

@monius1
Copy link

monius1 commented Nov 15, 2014

This method seems to be working on versions of android up to 4.2.1 as I've heard the interface IExtendedNetworkService has been removed from the SDK since then (due to security reasons). Though is there anybody knows how to make original dialer dialog invisible ?

@umeshongithub
Copy link

@lephuhung
Copy link

@umeshongithub: it not working on android 5.1.1, can you upgrade code ?

@ah-dev-med
Copy link

This would only be possible in an Android version earlier than 4.2.2. In 4.2.2 the IExtendedNetworkService seems to have been removed by Google due to security risk. See issue posted at Google – https://code.google.com/p/android/issues/detail?id=57120

https://android.googlesource.com/platform/packages/apps/Phone/+/f9abded9b7253c797af8b01102c153227b09446f%5E!/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment