我创建了一个简单的应用程序来获取来电号码,工作并向我显示带有 toast 的号码,但我想将此变量传递给我的主要活动进行处理...示例在呼叫管理员号码时发送短信。我尝试此代码但没有数据显示在我的用户界面中...有什么想法吗?对此有帮助吗?
public class IncomingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
Log.i("IncomingCallReceiver",bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.i("IncomingCallReceiver","State: "+ state);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("IncomingCallReceiver","Incomng Number: " + phonenumber);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
Intent intentsend=new Intent("update");//Send data to update UI
intentsend.putExtra( "data","Admin call..."+info);
context.sendBroadcast(intentsend);
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
我的主要活动是......
@Override
public void onResume() {
super.onResume();
updateReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intentsend) {
//get extras, do some stuff
String data = intentsend.getStringExtra("data");
textphone.setText(data);
}
};
IntentFilter updateIntentFilter=new IntentFilter("update");
registerReceiver(updateReceiver, updateIntentFilter);
}