Created
April 30, 2017 10:49
-
-
Save Anindya666/f3d87f25bf6c6ff443405aef591c0917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.theeeceguy.eqresq; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.provider.Telephony; | |
import android.support.v7.app.AlertDialog; | |
import android.telephony.SmsMessage; | |
import android.util.Log; | |
import android.widget.Toast; | |
public class SmsListener extends BroadcastReceiver { | |
String msgBody; | |
String msg_from; | |
@Override | |
public void onReceive(final Context context, Intent intent) { | |
// 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 = new SmsMessage[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 = new Intent(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"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment