通过广播实现对特定号码的拦截,写两个按钮,一个是正常拨号,一个是拨号后进行拦截
注意要在配置文件内添加权限
样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="call" android:text="正常拨号" />
<Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="block" android:text="加入拦截" /> </LinearLayout>
|
Activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.example.broadcastreceiver;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt1=(Button)findViewById(R.id.bt1); Button bt2=(Button)findViewById(R.id.bt2); }
public void call(View v){ Intent i = new Intent("正常拨号123456"); sendOrderedBroadcast(i,null); }
public void block(View v){ Intent i = new Intent("被拦截的拨号123456"); sendOrderedBroadcast(i,null); } }
|
接收者类
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.example.broadcastreceiver;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String str=intent.getAction().toString(); Toast.makeText(context,"接收到的广播信息:"+str, Toast.LENGTH_SHORT).show(); } }
|
拦截类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.example.broadcastreceiver;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast;
public class MyBlock extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String str1=intent.getAction().toString(); Toast.makeText(context,"拦截到的广播信息:"+str1, Toast.LENGTH_SHORT).show(); abortBroadcast(); Toast.makeText(context,"已清除广播", Toast.LENGTH_SHORT).show(); } }
|
manifest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:dist="http://schemas.android.com/apk/distribution" package="com.example.broadcastreceiver">
<dist:module dist:instant="true" /> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter android:priority="0"> <action android:name="正常拨号123456"/> <action android:name="被拦截的拨号123456"/> </intent-filter> </receiver>
<receiver android:name=".MyBlock" android:enabled="true" android:exported="true"> <intent-filter android:priority="1"> <action android:name="被拦截的拨号123456"/> </intent-filter> </receiver>
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest>
|
结果