我想知道是否有任何方法可以将字符串从活动传递到扩展phoneStatelistener的类。
在发送结束时,我使用了
Intent pass = new Intent(ListActivity.this,CallHelper.class);
pass.putExtra("name", name);
并在接收端..
public class CallHelper {
private class CallStateListener extends PhoneStateListener {
int flag;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String name = getintent().getStringExtra();
}
但它在getintent()部分显示错误,有没有其他方法?
答案 0 :(得分: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
}
}
}
答案 1 :(得分: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;
}
...
}
希望能帮到你!