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, "12345@abdcde.com"); // 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>