首先,新建一个类,继承于BroadcastReceive,然后去配置Manifest.xml
1 <receiver android:name=".PhoneOnReceice"> 2 <intent-filter> 3 <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> 4 </intent-filter> 5 </receiver>
然后加上android.permission.PROCESS_OUTGOING_CALLS权限(一定要记得加,不要加错了)
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
接下来写业务逻辑就行了,那就简单了
首先是MainActicity:
1 public class MainActivity extends Activity implements View.OnClickListener { 2 3 EditText et; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 9 Button btn = (Button)findViewById(R.id.btn); 10 et = (EditText) findViewById(R.id.et); 11 btn.setOnClickListener(this); 12 } 13 14 @Override 15 public void onClick(View view) { 16 String str = et.getText().toString().trim(); 17 SharedPreferences sp= getSharedPreferences("config",0); 18 sp.edit().putString("ipnumber",str).commit(); //一定记得提交 19 Toast.makeText(this,"ip设置成功",Toast.LENGTH_SHORT).show(); 20 } 21 }
然后就是我们刚新建的那个类了
1 public class PhoneOnReceice extends BroadcastReceiver { 2 @Override 3 public void onReceive(Context context, Intent intent) { 4 SharedPreferences sp = context.getSharedPreferences("config",0); 5 String ip = sp.getString("ipnumber",""); 6 String currentNumber = getResultData();//获取当前要拨打的电话号码 7 System.out.println("广播调用成功"); 8 if (currentNumber.startsWith("0")){ 9 System.out.print("进入if中"); 10 setResultData(ip+currentNumber); 11 } 12 } 13 }
当Mainfest.xml中的action执行的时候,就会执行BroadcastReceiver子类PhoneOnReceive中的onReceice方法。
注意其中的getResultData():是用来得到当前输入的手机号码的
时间: 2024-10-11 06:16:05