-
-
Save victorkifer/8616266 to your computer and use it in GitHub Desktop.
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); | |
} | |
} |
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
What about manifest? How can I use this service?
How about reading USSD response in 4.2.2?
Does this work in latest android versions like 4.0+ ?
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 ?
Checkout my blog to read USSD messages http://umeshisran4android.blogspot.in/2015/11/how-to-readparse-ussd-messages.html
@umeshongithub: it not working on android 5.1.1, can you upgrade code ?
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
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.