dreamer1989
dreamer1989

Reputation: 1115

start broadcastreceiver

Here I am trying to read the message and toast it I have seen various examples where there is a separate class that extends BroadcastReceiver but they have not mentioned how to start this class(do we use startactivity() or somthing else). I have posted the code that I dowladed through a link from O'Reilly's cookbook. I've tried to sms from ddms but it doesn't show toast of message. Any help is appreciated as this is my first time with BroadcastReceiver.

invitationSMSreciever.java

package com.SMS;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class invitationSMSreciever extends BroadcastReceiver {

    final String TAG = "BombDefusalApp";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String message = "";
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                message = msgs[i].getMessageBody();
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                if (msgs[i].getMessageBody().equalsIgnoreCase("Invite")) {
                    // Intent myIntent = new Intent(MainMenu.this,
                    // com.bombdefusal.ReceivedSMSActivity.class);
                    Intent myIntent = new Intent();
                    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    myIntent.setAction("com.example.helloandroid.INVITE");
                    context.startActivity(myIntent);
                }
            }
        }
    }
}

MainMenu

package com.SMS;

import com.SMS.R;

import android.app.Activity;
import android.os.Bundle;

public class MainMenu extends Activity {

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

ReceivedSMSActivity

package com.SMS;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.SMS.R;

public class ReceivedSMSActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startService(new Intent ("com.android.PLAY"));
        setContentView(R.layout.invite);
    }
    public boolean onKeyDown(int keyCode, KeyEvent service) {
        stopService(new Intent("com.bombdefusal.START_AUDIO_SERVICE"));
        finish();
        return true;
    }
}

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.SMS" android:versionCode="1" android:versionName="1.0">
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainMenu" android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.SMS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="com.SMS.ReceivedSMSActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.example.helloandroid.INVITE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

        <receiver android:name="com.SMS.invitationSMSreciever"
                  android:enabled="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>          
        </receiver>
    </application>
</manifest> 

Upvotes: 1

Views: 595

Answers (1)

Yury
Yury

Reputation: 20936

You have two options to do this:

  1. Register you broadcast receiver statically in the AndroidManifest file. Thus, it will be called automatically.
  2. Register you broadcast receiver dynamically in your code using registerReceiver() method. In this case, this method should be paired with unregisterReceiver() where you unregister your receiver.

Usually, if broadcast receiver is implemented as a separate class then it usually registered statically in AndroidManifest file. I guess in you case you should just add the following lines to your file:

<receiver android:name=".invitationSMSreciever" android:exported="true" > 
  <intent-filter android:priority="1000"> 
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  </intent-filter> 
</receiver>

Upvotes: 1

Related Questions