如何用Java阻止短信发送者?

发布于 2025-01-16 00:42:55 字数 5250 浏览 2 评论 0原文

我使用 Blocked Number Contract 编写了一个 Java 应用程序,但它崩溃了。我不明白如何使用该应用程序作为默认应用程序。我有一个用于检测传入短信中的垃圾邮件的应用程序,它读取最后一条短信,将其输出并进行预测。如何屏蔽该短信的发件人号码?我使用了封锁号码合同,但我不能。如何正确将应用程序设置为默认值。或者还有其他的拦截方法吗?请帮我!

MainActivity.java 中的一段代码

 if (phone_number.contains("+7")) {
            AsyncHttpClient client = new AsyncHttpClient();
            String url_ad = String.format("https://get-region-by-number.herokuapp.com/get_region/%1$s", phone_number);
            myTextView6.setText("Регион отправителя:");

            client.get(url_ad, new TextHttpResponseHandler() {

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    myTextView5.setText("");
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    myTextView5.setText(StringEscapeUtils.unescapeJava(responseString.replaceAll("\"", "")));

                    ContentValues values = new ContentValues(); // there 's a crash
                    values.put(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "[email protected]"); // there 's a crash
                    Uri uri = getContentResolver().insert(BlockedNumberContract.BlockedNumbers.CONTENT_URI, values); // there 's a crash

                }
            });
        }

 private boolean checkDefaultSettings() {

        boolean isDefault = false;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {

            if (!Telephony.Sms.getDefaultSmsPackage(this).equals(getPackageName())) {

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("This app is not set as your default messaging app. Do you want to set it as default?")
                        .setCancelable(false)
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();

                            }
                        })
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @TargetApi(19)
                            public void onClick(DialogInterface dialog, int id) {
                                Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                                intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getPackageName());
                                startActivity(intent);

                            }
                        });
                builder.show();

                isDefault = false;
            } else
                isDefault = true;
        }
        return isDefault;
    }


    private String isMyAppLauncherDefault() {
        final IntentFilter filter = new IntentFilter(Intent.ACTION_DEFAULT);
        filter.addCategory(Intent.CATEGORY_DEFAULT);

        List<IntentFilter> filters = new ArrayList<IntentFilter>();
        filters.add(filter);
        final String myPackageName = getPackageName();
        List<ComponentName> activities = new ArrayList<ComponentName>();
        final PackageManager packageManager = (PackageManager) getPackageManager();

        packageManager.getPreferredActivities(filters, activities, null);
        for (ComponentName activity : activities) {
            if (myPackageName.equals(activity.getPackageName())) {
                return "1";
            }
        }
        return "0";

    }

Manifest.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.mysmsapp">

    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="SPAM Detector"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MySMSApp">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:icon="@drawable/face">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

I wrote a Java application using Blocked Number Contract, but it crashes. I don't understand how to use the application as the default application. I have an application for detecting spam in incoming SMS, it reads the last SMS, outputs it and makes a prediction. How can I block the sender's number of this sms? I use Blocked Number Contract, but I can't. How to do set application as default correctly. Or are there other blocking methods? Please help me!

A piece of code from MainActivity.java

 if (phone_number.contains("+7")) {
            AsyncHttpClient client = new AsyncHttpClient();
            String url_ad = String.format("https://get-region-by-number.herokuapp.com/get_region/%1$s", phone_number);
            myTextView6.setText("Регион отправителя:");

            client.get(url_ad, new TextHttpResponseHandler() {

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    myTextView5.setText("");
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    myTextView5.setText(StringEscapeUtils.unescapeJava(responseString.replaceAll("\"", "")));

                    ContentValues values = new ContentValues(); // there 's a crash
                    values.put(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "[email protected]"); // there 's a crash
                    Uri uri = getContentResolver().insert(BlockedNumberContract.BlockedNumbers.CONTENT_URI, values); // there 's a crash

                }
            });
        }

 private boolean checkDefaultSettings() {

        boolean isDefault = false;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {

            if (!Telephony.Sms.getDefaultSmsPackage(this).equals(getPackageName())) {

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("This app is not set as your default messaging app. Do you want to set it as default?")
                        .setCancelable(false)
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();

                            }
                        })
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @TargetApi(19)
                            public void onClick(DialogInterface dialog, int id) {
                                Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                                intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getPackageName());
                                startActivity(intent);

                            }
                        });
                builder.show();

                isDefault = false;
            } else
                isDefault = true;
        }
        return isDefault;
    }


    private String isMyAppLauncherDefault() {
        final IntentFilter filter = new IntentFilter(Intent.ACTION_DEFAULT);
        filter.addCategory(Intent.CATEGORY_DEFAULT);

        List<IntentFilter> filters = new ArrayList<IntentFilter>();
        filters.add(filter);
        final String myPackageName = getPackageName();
        List<ComponentName> activities = new ArrayList<ComponentName>();
        final PackageManager packageManager = (PackageManager) getPackageManager();

        packageManager.getPreferredActivities(filters, activities, null);
        for (ComponentName activity : activities) {
            if (myPackageName.equals(activity.getPackageName())) {
                return "1";
            }
        }
        return "0";

    }

Manifest.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.mysmsapp">

    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="SPAM Detector"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MySMSApp">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:icon="@drawable/face">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文