Question

i want to know if there is any way to pass a string from an activity to a class which extends phoneStatelistener.

in sending end, i used

    Intent pass = new Intent(ListActivity.this,CallHelper.class);
    pass.putExtra("name", name);

and in the receiving end..

 public class CallHelper {

     private class CallStateListener extends PhoneStateListener {
         int flag;

         @Override
         public void onCallStateChanged(int state, String incomingNumber) {
             String name = getintent().getStringExtra();
         }

but it showing error in getintent() part, is there any alternate way?

Was it helpful?

Solution

You can't call getIntent() in a class that extends PhoneStateListener. If CallHelper is an activity, what I suggest is to override the default constructor of your CallStateListener.

public class CallHelper extends Activity{

    String name;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.call_helper_layout);
        Bundle extras = getIntent().getExtras();
        if(extras != null){
            name = extras.getString("name");
        }
        CallStateListener c = new CallStateListener(name);
    }

    private class CallStateListener extends PhoneStateListener {
        int flag;
        String name;

        public CallStateListener(String name){
            this.name = name;
        }

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
                 //do stuff with name
        }           
    }
}

OTHER TIPS

I always do it like this: I create the class and then use a setter to set the value. Sending an Intent is a bit much in my opinion...

the Activity:

class SomeActivity extends Activity {

    public void onCreate(Bundle savedInstanceState){
        CallStateListener listener = new CallStateListener();
        listener.setFlag(myValue);
    }

}

the Listener:

class CallStateListener extends PhoneStateListener {
     int flag;

     @Override
     public void onCallStateChanged(int state, String incomingNumber) {
         String name = getintent().getStringExtra();
     }

     public void setFlag(int flag) {
        this.flag = flag;
     }

     ...

}

Hope that helps you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top