NullPointerException
NullPointerException

Reputation: 4008

how to implement calling keypad event listener in android

I want to implement my BroadcastReceiver on pressing some key combination (say if i dial 1234 from my keypad) them my BroadcastReceiver will be called. By which i can launch my activity ?

Here is how i fixed this

Here is how i fixed this

public class MyKeypadListener extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub



     if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {

         String number = getResultData();   
         if (number!=null) {

            if(number.equals("1234")){

                 setResultData(null);
                 Intent newintent = new Intent(context,SettingsActivity.class);
                 newintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 context.startActivity(newintent);
                       }

             }


          }
        }

}

and in the manifest i hv added..

**<receiver android:name=".receivers.MyKeypadListener">
            <intent-filter >
                    <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
                   <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>**

Upvotes: 1

Views: 2553

Answers (2)

NullPointerException
NullPointerException

Reputation: 4008

Here is how I fixed this:

public class MyKeypadListener extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub



 if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {

     String number = getResultData();   
     if (number!=null) {

        if(number.equals("1234")){

             setResultData(null);
             Intent newintent = new Intent(context,SettingsActivity.class);
             newintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             context.startActivity(newintent);
                   }

         }


      }
        }

}

and in the manifest I have added:

<receiver android:name=".receivers.MyKeypadListener">
            <intent-filter >
                    <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
                   <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>

Upvotes: 2

AndroDev
AndroDev

Reputation: 3284

You will have to implement TextWatcher for the edit text where you are entering these combination. This API has some callback methods where you can easily check your combination.

Upvotes: 0

Related Questions