Reputation: 61
I am writing a program where the main activity in my app should be invoked when a particular SMS arrives on the phone. I have already registered a BroadcastReceiver
and the intent to invoke the activity is in the onReceive()
method. The problem is, everytime I send this particular SMS, I get a Force close. Reading the logcat, I see the following NullPoint Exception:
10- 20 02:07:16.558: E/AndroidRuntime(1200): java.lang.RuntimeException: Unable to start receiver edu.example.prankssms3.SMSReceiverActivity3: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=edu.example.prankssms3.action.STARTMUSIC flg=0x10000000 }
But as far as am concerned everything is done right. Would anyone please tell me where the problem is? Thank you in advance.
Here is the manifestfile:
<activity
android:name=".MainActivity3"
android:label="@string/title_activity_main_activity3" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="edu.example.prankssms3.action.STARTMUSIC" />
</intent-filter>
</activity>
<receiver android:name="SMSReceiverActivity3">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
here is the broadcastreceiver
public void onReceive(Context context, Intent intent) {
// get the message passed in
Bundle bdl = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if(bdl != null){
// retrieve the sms message received
Object[] pdus = (Object[]) bdl.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
str += msgs[i].getMessageBody().toString();
}
if(str.equals("FIRE_THE_MISSILES")){
// The pranking comes here
// start the activity to play the music and send sms message
Intent launchActivity = new Intent();
launchActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchActivity.setAction("edu.example.prankssms3.action.STARTMUSIC");
context.startActivity(launchActivity);
}
}
}
and here is the main class, which is also the class to be invoked by the intent:
public class MainActivity3 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main3, menu);
return true;
}
}
Upvotes: 4
Views: 5490
Reputation: 7797
Try to change your <intent-filter>
like this:
<intent-filter>
<action android:name="edu.example.prankssms3.action.STARTMUSIC" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Upvotes: 3
Reputation: 1192
Lanching an activity from a broadcast receiver is an not usual operation. Trying adding FLAG_ACTIVITY_NEW_TASK to the Intent you use for startActivity().
Calling startActivity()
from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK
flag. Hopefully this will solve your problem.
Upvotes: 0