| packagecom.theeeceguy.eqresq; | |
| importandroid.content.BroadcastReceiver; | |
| importandroid.content.Context; | |
| importandroid.content.DialogInterface; | |
| importandroid.content.Intent; | |
| importandroid.content.SharedPreferences; | |
| importandroid.os.Build; | |
| importandroid.os.Bundle; | |
| importandroid.provider.Telephony; | |
| importandroid.support.v7.app.AlertDialog; | |
| importandroid.telephony.SmsMessage; | |
| importandroid.util.Log; | |
| importandroid.widget.Toast; | |
| publicclassSmsListenerextendsBroadcastReceiver { | |
| String msgBody; | |
| String msg_from; | |
| @Override | |
| publicvoidonReceive(finalContextcontext, Intentintent) { | |
| // TODO: This method is called when the BroadcastReceiver is receiving | |
| // an Intent broadcast. | |
| if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){ | |
| Bundle bundle = intent.getExtras(); //---get the SMS message passed in--- | |
| SmsMessage[] msgs =null; | |
| if (bundle !=null){ | |
| //---retrieve the SMS message received--- | |
| try{ | |
| Object[] pdus = (Object[]) bundle.get("pdus"); | |
| msgs =newSmsMessage[pdus.length]; | |
| for(int i=0; i<msgs.length; i++){ | |
| msgs[i] =SmsMessage.createFromPdu((byte[])pdus[i]); | |
| msg_from = msgs[i].getOriginatingAddress(); | |
| msgBody = msgs[i].getMessageBody(); | |
| } | |
| if(msgBody.contains("#earthquake")) { | |
| if(msg_from.contains("+8801553721331")) | |
| Toast.makeText(context, "Earthquake Alert!", Toast.LENGTH_SHORT).show(); | |
| intent =newIntent(context, DialogActivity.class); | |
| intent.putExtra("smsbody", msgBody); | |
| intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| context.startActivity(intent); | |
| } | |
| }catch(Exception e){ | |
| Log.d("Exception caught",e.getMessage()); | |
| } | |
| } | |
| } | |
| //throw new UnsupportedOperationException("Not yet implemented"); | |
| } | |
| } |
SpaceApps is a NASA incubator innovation program.