我正在从广播接收器检索传出号码并尝试通过方法getNumber()将其发送到活动,但是该值是空的。我的代码如下在活动类中,String phonenumber为null
BroadcastReciever Class:
public class OutgoingBroadcastReceiver extends BroadcastReceiver {
String phonenumber = null;
@Override
public void onReceive(Context context, Intent intent) {
phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL"))
{
Log.i("System out", "IN OUTGOING CALL......... :IF");
MyPhoneStateListener phoneListener = new MyPhoneStateListener(
context);
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
} else {
Log.i("System out", "IN INCOMING CALL.........:else:receiver");
}
public String getNumber()
{
return phonenumber;
}
活动类:
public class OutgoingCallScreenDisplay extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.outgoing_main);
OutgoingBroadcastReceiver outreciever = new OutgoingBroadcastReceiver();
String phonenumber= outreciever.getNumber();//this is coming out NULL needs to be the outgoing number
}
答案 0 :(得分:0)
立即想到两件事:
onReceive
方法。答案 1 :(得分:0)
使用putExtra
答案 2 :(得分:0)
电话号码是null因为你创建了一个新的broadcastreceiver实例,其值没有被启动。你可以收到电话号码。仅在“onReceive”中,如果你想进一步传递,你应该开始新的活动
请参阅:
首先,你要么使用广播接收器,要么使用电话状态监听器。在你的情况下,如果你使用广播接收器,那么你需要额外的意图:EXTRA_PHONE_NUMBER
和许可:处理outgoin电话。就是这样.....