0

I'm trying to add some features to my android application with USSD codes. assume when user type *#12345# on home screen in my app. then i need to show a dialog or send user to a another screen in my app.

how can i accomplish this task ?

Bishan
  • 15,211
  • 52
  • 164
  • 258

1 Answers1

1

Try following code.

ussd_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

And your MainActivity.java will have following code

package com.example.ussdcodetest;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class USSDTestActivity extends Activity {

TextView txt;
RelativeLayout rel;
String s = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ussdtest);

    txt = (TextView) findViewById(R.id.textView);
    rel = (RelativeLayout) findViewById(R.id.relative);

    txt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("USSD", "CLICKED");
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                    .toggleSoftInput(InputMethodManager.SHOW_FORCED,
                            InputMethodManager.HIDE_IMPLICIT_ONLY);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_ussdtest, menu);
    return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    Log.d("USSD", "Key Down  :" + keyCode + " String : " + s);
    s += (char) event.getUnicodeChar();

    if (s.equals("action")) {
        Toast.makeText(USSDTestActivity.this, "Action will be performed",
                Toast.LENGTH_LONG).show();
        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(rel.getWindowToken(), 0);
    }
    return super.onKeyDown(keyCode, event);
}
}

What my code will do is, it will take input from keyboard which will be shown on TextView's click event, and when input will be matched with action word, it will hide keyboard and Toast message on screen. You can write your own code inside that if condition.

Note: There must be some action to show or hide keyboard on activity, that is why I use onClickEvent of TextView. You can use as per your need. Best idea is use Gesture.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93