how to implement calling keypad event listener in

2019-03-06 14:20发布

问题:

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>**

回答1:

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>


回答2:

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.