0
中使用的變量中我正在查看BroadcastReceiver
和Activity
,我沒有找到正確的方式來使用我在Activity
中的BroadcastReceiver
收到的電話號碼。這是我用來攔截來電號碼,並在Toast
可視化代碼:將收到的電話號碼保存到活動
public class CustomBroadcastReceiver extends BroadcastReceiver {
String ophoneNumber;
@Override
public void onReceive(Context context, Intent incoming) {
Bundle bundle = incoming.getExtras();
ophoneNumber= bundle.getString("incoming_number");
Toast.makeText(context, ophoneNumber, Toast.LENGTH_LONG).show();
}
}
清單部分:
<receiver android:name=".CustomBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
我的問題是,我必須使用在Activity
來電號碼是正在運行,所以我也看到了有關可能把BroadcastReceiver
的Activity
像這裏面:
public void monitorIncomingCalls(){
INcall = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
final Bundle extras = intent.getExtras();
if(intent.getAction().equalsTelephonyManager.ACTION_PHONE_STATE_CHANGED))
inphoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
};
registerReceiver(INcall, new IntentFilter(Intent.ACTION_ANSWER));
}
但這個我沒有inphoneNumber變量,我想因爲我沒有正確註冊BroadcastReceiver
,因爲我錯過了許可。 我沒有在網上找到一個正在爲我想要的東西工作的例子,那基本上放在了我的號碼Activity
中的一個變量中。