Block incoming texts (Android)

I am working on an application that we hope will be able to block incoming text messages (depending on user settings), but I am having problems detecting incoming messages.

Could you take a look at my codes and tell me what I'm doing wrong? I look through other questions similar to this, but I cannot find any detailed answer or sufficient information for the link.

import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; public class SmsReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){ Bundle bundle = intent.getExtras(); if (bundle != null){ abortBroadcast(); } } } } 

Here is my manifest

 <receiver android:name=".listener.SmsReceiver"> <intent-filter android:priority="100"> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> 

I am following the MobiForge tutorial ( http://mobiforge.com/developing/story/sms-messaging-android ) as well as questions here:

How to block an incoming message in android?

Android - listen to incoming SMS messages

Can someone point me in the right direction? I would appreciate.

+6
source share
1 answer

Here I use to block incoming texts. This is how I answered my question.


SmsReceiver.java

 import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.widget.Toast; public class BroadCastReceiver extends BroadcastReceiver { /** Called when the activity is first created. */ private static final String ACTION = "android.provider.Telephony.SEND_SMS"; public static int MSG_TPE=0; public void onReceive(Context context, Intent intent) { String MSG_TYPE=intent.getAction(); if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) { // Toast toast = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG); // toast.show(); Bundle bundle = intent.getExtras(); Object messages[] = (Object[]) bundle.get("pdus"); SmsMessage smsMessage[] = new SmsMessage[messages.length]; for (int n = 0; n < messages.length; n++) { smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]); } // show first message Toast toast = Toast.makeText(context,"BLOCKED Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG); toast.show(); abortBroadcast(); for(int i=0;i<8;i++) { System.out.println("Blocking SMS **********************"); } } else if(MSG_TYPE.equals("android.provider.Telephony.SEND_SMS")) { Toast toast = Toast.makeText(context,"SMS SENT: "+MSG_TYPE , Toast.LENGTH_LONG); toast.show(); abortBroadcast(); for(int i=0;i<8;i++) { System.out.println("Blocking SMS **********************"); } } else { Toast toast = Toast.makeText(context,"SIN ELSE: "+MSG_TYPE , Toast.LENGTH_LONG); toast.show(); abortBroadcast(); for(int i=0;i<8;i++) { System.out.println("Blocking SMS **********************"); } } } } 

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="APP.PACKAGE.NAMEHERE" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:resizeable="true" android:anyDensity="true" /> <uses-feature android:name="android.hardware.telephony" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.WRITE_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECEIVE_MMS" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".APPACTIVITYHERE" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden" > <service android:name=".MyService" android:enabled="true"/> <receiver android:name="SmsReceiver"> <intent-filter android:priority="2147483647"> <action android:name="android.provider.Telephony.SMS_SENT"/> </intent-filter> </receiver> <service android:name=".MyServiceSentReceived" android:enabled="true"/> <receiver android:name="SmsReceiver"> <intent-filter android:priority="2147483645"> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> </application> 

The main thing to cancel the manifest is the service block, receiver and permission block.

+10
source

Source: https://habr.com/ru/post/918970/


All Articles