Pages

Sunday, August 20, 2017

Dial USSD Code

We can handle ussd code at run time in android programatically for getting response of any ussd code.


==> activity_ussdcode.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:titleTextColor="@color/white"
        app:titleTextColor="@color/white"></android.support.v7.widget.Toolbar>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="20dp"
        android:layout_marginTop="50dp"
        android:orientation="vertical">

        <EditText
            android:id="@+id/edt_ussdcode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter USSD Code like *111*2" />


        <Button
            android:id="@+id/btn_dial"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Dial" />
    </LinearLayout>

</LinearLayout>

==> USSDCodeActivity.java

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zealous.demoapps.R;
import com.zealous.demoapps.service.USSDMainService;

import butterknife.BindView;
import butterknife.ButterKnife;
public class USSDCodeActivity extends AppCompatActivity {


    @BindView(R.id.btn_dial)
    Button btn_dial;

    @BindView(R.id.edt_ussdcode)
    EditText edt_ussdcode;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ussdcode);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Dial USSD code");
        startService(new Intent(this, USSDMainService.class));
        clickEvent();
    }

    private void clickEvent() {
        btn_dial.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {

                if (!edt_ussdcode.getText().toString().equalsIgnoreCase("")) {
//                    dailNumber("*111*2");                    dailNumber(edt_ussdcode.getText().toString());
                } else {
                    Toast.makeText(USSDCodeActivity.this, "Please Enter USSD code", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void dailNumber(String code) {
        String ussdCode = "*" + code + Uri.encode("#");
        startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode)));
    }
}

No comments:

Post a Comment