0

I want to block incoming SMS message as long as my application is running . I could achieve that but the problem is after I close the app or even restart or even uninstall it the user will not be able to receive SMS message anymore . So How can I make the application to block incoming SMS only when it's running and when it gets closed or uninstalled etc.. to stop blocking SMS. Here's my code :

BroadCastReceiver.Java

package com.example.sms;
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 
{

    public void onReceive(Context context, Intent intent)
    {
     abortBroadcast();
     }
    }

MainActivity.java

package com.example.sms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

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

Manifiest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".BroadCastReceiver">
    <intent-filter android:priority="2147483647">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

        <activity
            android:name="com.example.sms.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Note: I've tested the application on android 2.2 on my android emulator "Sent SMS messages using Telnet"

AlphaCode
  • 439
  • 1
  • 3
  • 17

1 Answers1

1

Check if your app is running then only abort broadcast receiver else not

//define this variable above onReceive() with default value as false;
boolean appRunningInBack=false;

ActivityManager am = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();

if(packageName.equalIgnoreCase("your app package name") || appRunningInBack)
{    
    appRunningInBack=true;
    abortBroadCast();
} else {

}

Also add Permission in AndroidManifest.xml

<uses-permission android:name="android.permission.GET_TASKS" />

After this when your app goes in background.or while running .this variable will be true.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Meenal
  • 2,879
  • 5
  • 19
  • 43
  • Actually I used contains since equallIgnoreCase gave me an error . I also replaced mContext with Context because it said mContext is unidentified or something like that . – AlphaCode Jan 07 '15 at 12:40
  • But please note that a permission shall be added which is : – AlphaCode Jan 07 '15 at 12:44
  • 1
    ya..i forget to mention that..glad it worked for you..i will edit my answer..thanks!! – Meenal Jan 07 '15 at 12:44
  • 1
    i suggest you to block sms only when your app is running..not in other cases. – Meenal Jan 07 '15 at 12:46
  • 1
    Well That's exactly what I want to do because otherwise the application will be considered malicious – AlphaCode Jan 07 '15 at 12:47
  • but I've got another problem . When the application is running in the background the receiver seems not to work . Can I do anything about that ?? – AlphaCode Jan 07 '15 at 12:49
  • I think due to getRunningTasks that doesn't consider applications running in the background as running tasks , is there's any alternative for it that does the job ?? – AlphaCode Jan 07 '15 at 12:51
  • yes before going in background you can save the state of the app..through a boolean.. – Meenal Jan 07 '15 at 12:54
  • Sorry I still new to the world of java, can you show an example please ?? – AlphaCode Jan 07 '15 at 13:03