как взять и показать ответ USSD в Textview

Я хочу создать приложение с USSD-кодом, но не знаю, как взять и показать USSD-ответ в форме TextView.

Я пробовал этот toturial: Использование IExtendedNetworkService для получения ответа USSD в Android< /а>

Я сделал все классы в этой теме, как показано ниже:

введите здесь описание изображения

Затем я создал следующее MainActivity:

public class MainActivity extends ActionBarActivity {
    private EditText cartnumber_edittext,cartpass_edittext;
    private Button ussd_btn;
    private String cartnumber,cartpass;
    private TextView uss_response_txt;

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

        cartnumber_edittext=(EditText) findViewById(R.id.cartnumber_edittext);
        cartpass_edittext=(EditText) findViewById(R.id.cartpass_edittext);
        ussd_btn=(Button) findViewById(R.id.ussd_btn);
        uss_response_txt=(TextView) findViewById(R.id.uss_response_txt);

        ussd_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {

                cartnumber=cartnumber_edittext.getText().toString();
                cartpass=cartpass_edittext.getText().toString();

                USSDDumbExtendedNetworkService.mActive = false;

                String USSD_code = "tel:" + "*720*2*1*2*0*"+cartnumber+"*"+cartpass+"#";
                //Toast.makeText(getApplicationContext(), USSD_code, Toast.LENGTH_SHORT).show();
                Intent launchCall = new Intent(Intent.ACTION_CALL,
                        Uri.parse("tel:" + Uri.encode(USSD_code)));
                launchCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                launchCall.addFlags(Intent.FLAG_FROM_BACKGROUND);
                startActivity(launchCall);


                USSDDumbExtendedNetworkService.mActive = true;
                USSDDumbExtendedNetworkService.mRetVal = null;


            }
        });

    }

}

Не знаю как взять и показать USSD ответ в TextView по String. Может кто-нибудь мне помочь?


person Sadra Isapanah Amlashi    schedule 13.09.2015    source источник


Ответы (1)


попробуйте в классе обслуживания:

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.IBinder;
import android.os.PatternMatcher;
import android.os.RemoteException;
import android.util.Log;

import com.android.internal.telephony.IExtendedNetworkService;
import com.codedemigod.ussdinterceptor.R;

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

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

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

и в классе приемника (который вы хотите):

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class CDBootCompleteRcv extends BroadcastReceiver {
    private String TAG = CDBootCompleteRcv.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "rcvd boot event, launching service");
        Intent srvIntent = new Intent(context, CDUSSDService.class);
        context.startService(srvIntent);
    }

}
person Milad gh    schedule 13.09.2015
comment
этот источник взят с этого сайта и протестирован - person Milad gh; 13.09.2015
comment
Тогда как насчет основного действия? CDBootCompleteRcv — моя основная деятельность? - person Sadra Isapanah Amlashi; 13.09.2015
comment
CDUSSDService является основным - person Milad gh; 13.09.2015
comment
вы можете скачать его полностью с здесь - person Milad gh; 13.09.2015
comment
у меня есть телеграмма @Milad_Ghazi - person Milad gh; 13.09.2015