欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【无标题】android号码

程序员文章站 2022-07-14 17:56:57
...

MainActivity.java:

public class MainActivity extends AppCompatActivity {
private EditText et;
private Button btn;
SharedPreferences sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et = findViewById(R.id.et);
    btn = findViewById(R.id.button);

    sp = getSharedPreferences("mingdan", MODE_PRIVATE);


    if (ContextCompat.checkSelfPermission(this, Manifest.permission.PROCESS_OUTGOING_CALLS)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.PROCESS_OUTGOING_CALLS},1
        );
    }else{
    }

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String num = et.getText().toString().trim();
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("number",num);
            editor.commit();
            Toast.makeText(MainActivity.this,"已设置拦截",Toast.LENGTH_SHORT).show();


        }
    });

}

}

MyReceiver.java:

public class MainActivity extends AppCompatActivity {
private EditText et;
private Button btn;
SharedPreferences sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et = findViewById(R.id.et);
    btn = findViewById(R.id.button);

    sp = getSharedPreferences("mingdan", MODE_PRIVATE);


    if (ContextCompat.checkSelfPermission(this, Manifest.permission.PROCESS_OUTGOING_CALLS)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.PROCESS_OUTGOING_CALLS},1
        );
    }else{
    }

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String num = et.getText().toString().trim();
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("number",num);
            editor.commit();
            Toast.makeText(MainActivity.this,"已设置拦截",Toast.LENGTH_SHORT).show();


        }
    });

}

}

activity_main

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:id="@+id/et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="设置拦截" />

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>


<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/Theme.Rhj220">
    <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter android:priority="1000">
            <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
        </intent-filter>
    </receiver>

    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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