title: How to block an sms sender in Java?
body: <p>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!</p>
  <p><strong>A piece of code from MainActivity.java</strong></p>
  <pre><code> if (phone_number.contains(&quot;+7&quot;)) {
              AsyncHttpClient client = new AsyncHttpClient();
              String url_ad = String.format(&quot;https://get-region-by-number.herokuapp.com/get_region/%1$s&quot;, phone_number);
              myTextView6.setText(&quot;Регион отправителя:&quot;);
  
              client.get(url_ad, new TextHttpResponseHandler() {
  
                  @Override
                  public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                      myTextView5.setText(&quot;&quot;);
                  }
  
                  @Override
                  public void onSuccess(int statusCode, Header[] headers, String responseString) {
                      myTextView5.setText(StringEscapeUtils.unescapeJava(responseString.replaceAll(&quot;\&quot;&quot;, &quot;&quot;)));
  
                      ContentValues values = new ContentValues(); // there 's a crash
                      values.put(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER, &quot;12345@abdcde.com&quot;); // 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 &gt;= android.os.Build.VERSION_CODES.KITKAT) {
  
              if (!Telephony.Sms.getDefaultSmsPackage(this).equals(getPackageName())) {
  
                  AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                  builder.setMessage(&quot;This app is not set as your default messaging app. Do you want to set it as default?&quot;)
                          .setCancelable(false)
                          .setNegativeButton(&quot;No&quot;, new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  dialog.dismiss();
  
                              }
                          })
                          .setPositiveButton(&quot;Yes&quot;, 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&lt;IntentFilter&gt; filters = new ArrayList&lt;IntentFilter&gt;();
          filters.add(filter);
          final String myPackageName = getPackageName();
          List&lt;ComponentName&gt; activities = new ArrayList&lt;ComponentName&gt;();
          final PackageManager packageManager = (PackageManager) getPackageManager();
  
          packageManager.getPreferredActivities(filters, activities, null);
          for (ComponentName activity : activities) {
              if (myPackageName.equals(activity.getPackageName())) {
                  return &quot;1&quot;;
              }
          }
          return &quot;0&quot;;
  
      }
  </code></pre>
  <p><strong>Manifest.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      package=&quot;com.example.mysmsapp&quot;&gt;
  
      &lt;uses-permission android:name=&quot;android.permission.READ_SMS&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.VIBRATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.RECEIVE_SMS&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot;/&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;SPAM Detector&quot;
          android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/Theme.MySMSApp&quot;&gt;
          &lt;activity
              android:name=&quot;.MainActivity&quot;
              android:exported=&quot;true&quot;
              android:icon=&quot;@drawable/face&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  
  &lt;/manifest&gt;
  </code></pre>