0

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?

4

2 回答 2

2

你不能getIntent()在一个类中调用extends PhoneStateListener。如果CallHelper是一个活动,我建议覆盖你的默认构造函数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
        }           
    }
}
于 2013-05-16T07:11:17.543 回答
0

我总是这样做:我创建类,然后使用 setter 设置值。在我看来,发送意图有点多……

活动:

class SomeActivity extends Activity {

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

}

听众:

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;
     }

     ...

}

希望对你有帮助!

于 2013-05-16T07:19:58.203 回答