实战演练——拦截史迪仔电话

拦截史迪仔电话(广播接收者)

  1. 创建程序:
    创建一个名为InterceptCall 的应用程序,指定包名为cn.itcast.interceptcall,设计用户交互界面。
    先把界面写好:(还有先准备一张图片 作为背景图,放在drawable 文件夹里面android:background="@drawable/sdz")
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:background="@drawable/sdz"
    tools:context="com.example.asus.interceptcall.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>

界面

  1. 编写界面交互代码:
    在MainActivity中编写的界面交互的代码,将要拦截的号码保存到SharedPreferences对象中
package com.example.asus.interceptcall;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
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);
        //创建SHaredPreferences对象
        sp=getSharedPreferences("config",MODE_PRIVATE);
    }
    public void click(View view){
        //获取用户输入的拦截号码
        String number=et_ipnumber.getText().toString().trim();
        //创建Editor对象,保存用户输入的拦截号码
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("number",number);
        editor.commit();
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
    }
}
  1. 监听广播事件
    创建一个广播接收者OutCallReceiver,用于接受外拨电话的广播
package com.example.asus.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) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        //获取拨打的电话号码
        String outcallnumber=getResultData();
        //创建sharedPreferces 对象,获取拦截号码
        SharedPreferences sp=context.getSharedPreferences("config",Context.MODE_PRIVATE);
        String number=sp.getString("number","");
        //判断是否拦截号码
        if(outcallnumber.equals(number)){
            //清除电话
            setResultData(null);
        }
    }
}
当我们输入完电话号码的时,Android系统会发送一个广播给电话拨号器的广播接收者,因此,想要拦截生效,就需要在Android Manifest.xml文件中注册广播接收者,来拦截外拨电话,由于涉及权限,所以还需要添加相应的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.asus.interceptcall">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".OutCallReceiver"
            android:enabled="true"
            android:exported="true"></receiver>
        <!--注册广播接收者-->
        <receiver android:name=".OutCallReceiver">
            <intent_filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent_filter>
        </receiver>
    </application>

    <!--设置权限-->
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

</manifest>

权限

  1. 运行程序

  2. 测试拦截

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是使用Python turtle库绘制史迪仔的代码和效果图: ```python import turtle # 设置画笔 t = turtle.Pen() t.pensize(5) t.speed(10) # 绘制头部 t.fillcolor('#FED85D') t.begin_fill() t.circle(100) t.end_fill() # 绘制眼睛 t.penup() t.goto(-40, 120) t.pendown() t.fillcolor('white') t.begin_fill() t.circle(30) t.end_fill() t.penup() t.goto(-30, 130) t.pendown() t.fillcolor('black') t.begin_fill() t.circle(15) t.end_fill() t.penup() t.goto(40, 120) t.pendown() t.fillcolor('white') t.begin_fill() t.circle(30) t.end_fill() t.penup() t.goto(30, 130) t.pendown() t.fillcolor('black') t.begin_fill() t.circle(15) t.end_fill() # 绘制嘴巴 t.penup() t.goto(-60, 60) t.pendown() t.right(45) t.circle(80, 90) # 绘制身体 t.penup() t.goto(-100, -100) t.pendown() t.fillcolor('#FED85D') t.begin_fill() t.right(135) t.forward(200) t.right(45) t.circle(80, 90) t.right(45) t.forward(200) t.right(135) t.circle(100, 180) t.end_fill() # 绘制手臂 t.penup() t.goto(-200, -100) t.pendown() t.fillcolor('#FED85D') t.begin_fill() t.right(45) t.forward(100) t.right(45) t.circle(80, 90) t.right(45) t.forward(100) t.right(135) t.circle(100, 180) t.end_fill() t.penup() t.goto(100, -100) t.pendown() t.fillcolor('#FED85D') t.begin_fill() t.right(135) t.forward(100) t.right(45) t.circle(-80, 90) t.right(45) t.forward(100) t.right(135) t.circle(-100, 180) t.end_fill() # 绘制脚 t.penup() t.goto(-100, -300) t.pendown() t.fillcolor('#FED85D') t.begin_fill() t.right(45) t.forward(100) t.right(45) t.circle(80, 90) t.right(45) t.forward(100) t.right(135) t.circle(100, 180) t.end_fill() t.penup() t.goto(100, -300) t.pendown() t.fillcolor('#FED85D') t.begin_fill() t.right(135) t.forward(100) t.right(45) t.circle(-80, 90) t.right(45) t.forward(100) t.right(135) t.circle(-100, 180) t.end_fill() # 隐藏画笔 t.hideturtle() # 等待点击关闭窗口 turtle.done() ``` 绘制效果图如下: ![史迪仔](https://i.loli.net/2021/08/12/7J5zvKZV8Q6q1fS.png)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Big-Winda

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值