工具:Android Studio
拦截史迪仔电话 (广播接收者的创建应用)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/sdz"
android:padding="15dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_ipnumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入拦截号码"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_ipnumber"
android:layout_centerHorizontal="true"
android:background="#ACD6FF"
android:onClick="click"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="保存拦截号码"
android:textSize="16sp"/>
</RelativeLayout>
MainActivity.java
package cn.itcast.interceptcall;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText et_ipnumber;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_ipnumber = (EditText)findViewById(R.id.et_ipnumber);
sp = getSharedPreferences("config",MODE_PRIVATE);
}
public void click(View view){
String number = et_ipnumber.getText().toString().trim();
SharedPreferences.Editor editor = sp.edit();
editor.putString("number",number);
editor.commit();
Toast.makeText(this,"保存成",Toast.LENGTH_SHORT).show();
}
}
OutCallReceiver.java
package cn.itcast.interceptcall;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
public class OutCallReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context,Intent intent){
String outcallnumber = getResultData();
SharedPreferences sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
String number = sp.getString("number","");
if(outcallnumber.equals(number)){
setResultData(null);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.interceptcall">
<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=".OutCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</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>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
</manifest>
界面效果
拦截史迪仔电话(自定义广播的发送与接收的应用)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/stitch"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="send"
android:layout_marginTop="50dp"
android:text="发送求救广播"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="#FFD2D2"
android:textSize="20sp"/>
</RelativeLayout>
MainActivity.java
package com.example.forhelp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view){
Intent intent = new Intent();
intent.setAction("Help_Stitch");
sendBroadcast(intent);
}
}
MyBroadcastReceiver.java
package com.example.forhelp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiver", "自定义的广播接收者,接收到了求救广播事件");
Log.i("MyBroadcastReceiver",intent.getAction());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.forhelp">
<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">
<intent-filter>
<action android:name="Help_Stitch"/>
</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>
界面效果
拦截史迪仔电话(自定义广播的发送、接收与拦截的应用)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/stitch_one"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="send"
android:layout_marginTop="80dp"
android:text="发送有序广播"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="#FBFBFF"
android:textSize="20sp"/>
</RelativeLayout>
MainActivity.java
package com.example.orderedbroadcast;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view){
Intent intent = new Intent();
intent.setAction("Intercept_Stitch");
sendOrderedBroadcast(intent,null);
}
}
MyBroadcastReceiverOne.java
package com.example.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverOne extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.i("MyBroadcastReceiverOne","自定义的广播接收者,接收到了求救广播事件");
}
}
MyBroadcastReceiverTwo.java
package com.example.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.i("MyBroadcastReceiverTwo","自定义的广播接收者,接收到了求救广播事件");
}
}
MyBroadcastReceiverThree.java
package com.example.orderedbroadcast;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverThree extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.i("MyBroadcastReceiverThree","自定义的广播接收者,接收到了求救广播事件");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orderedbroadcast">
<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=".MyBroadcastReceiverOne">
<intent-filter android:priority="1000">
<action android:name="Intercept_Stitch" />
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverTwo">
<intent-filter android:priority="600">
<action android:name="Intercept_Stitch" />
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverThree">
<intent-filter android:priority="200">
<action android:name="Intercept_Stitch" />
</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>
界面效果