Skip to content

Instantly share code, notes, and snippets.

@victorkifer
Created January 25, 2014 13:19

Revisions

  1. Victor Kifer created this gist Jan 25, 2014.
    79 changes: 79 additions & 0 deletions USSDNetworkService.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    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);
    }

    }