0

I wanted to intercept an incoming message so I created a class extending BroadcastReceiver(as given here) it worked but as I wanted to control the receiver(stop it when required) I implemented this but now when I exit the activity the receiver doesn't work.

How to implement the receiver such that it can be controlled and receive broadcast when the activity is not running?

Mainactivity

public class MainActivity extends Activity {
    Button StopB;
    IntentFilter STARTER;
    final BroadcastReceiver MSgR=new BR();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StopB=(Button) findViewById(R.id.button1);
        final BroadcastReceiver MSgR=new BR();
        STARTER=new IntentFilter();
        STARTER.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(MSgR, STARTER);

    StopB.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Log.i("Pressed", "UNRGSTR");
            unregisterReceiver(MSgR);
        }
    } ) ;


    }

BR.java

  public class BR extends BroadcastReceiver{
String TAG="DELETE BLOCK";
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        // do something here
         }

Manifest

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <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" >
        <activity
            android:name="com.example.delete_sms_2.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>
        <receiver android:name="BR"></receiver>
    </application>

</manifest>
Community
  • 1
  • 1
dreamer1989
  • 1,075
  • 3
  • 12
  • 29

1 Answers1

4

define when the receiver will start, you define when activity start then recive work, you need to change in menifest file , as like in this intent filter when sms recived, as like you change it accourding to ur need thanks

 <receiver android:name=".HellowordActivity" >
            <intent-filter > 
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
            </intent-filter> 
    </receiver> 
stackoverflow
  • 2,320
  • 3
  • 17
  • 21
  • if i do that i cant unregister receiver – dreamer1989 Apr 24 '13 at 06:38
  • you can do any thing in BroadcastReceiver . one time controller goes to ur BroadcastReceiver and match conditions , it depends upon you how you will use it – stackoverflow Apr 24 '13 at 06:43
  • if u don't want to add reciver that there is other method to do like that http://stackoverflow.com/questions/16182314/sms-broadcast-receiver-without-manifest – stackoverflow Apr 24 '13 at 06:46
  • the solution u mentioned works well but as i have mentioned earlier i wanted to unregister the receiver thats why i used the message of the second link.i am bale to receive the Broadcast in both the cases but in first i cant unregister and by the method of second link i am able to unregister but as the scope of the Broadcast object ends when i exist the activty the whole motive of receiving Broadcast is undone – dreamer1989 Apr 24 '13 at 06:56
  • insert unregisterReceiver in prefrence file and call when u want – stackoverflow Apr 24 '13 at 07:02
  • what is preference file ?. I used unregisterReceiver(onclick) it works when i used the solution u suggested the receiver still receives the Broadcast even after calling unregisterReceiver. – dreamer1989 Apr 24 '13 at 07:28
  • it will goes every time and at there you use if else condition , according to me it can't stop , i can be wrong but it will goes every time – stackoverflow Apr 24 '13 at 07:32
  • the problem as far as i see is that the way u suggested the class that extends Broadcast receiver is bound to be invoked independent of activity. but when we use the way i mentined in the mainactivity class above the scope of the receiver perishes as the activity exits by the solution u suggested the receiver is invoked at the manifest level that why i am having difficulty to unregister it – dreamer1989 Apr 24 '13 at 07:49