Guest User

Untitled

a guest
Jul 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. public class SmsReceiver extends BroadcastReceiver {
  2.  
  3. @Override
  4. public void onReceive(Context context, Intent intent) {
  5.  
  6. Bundle bundle = intent.getExtras();
  7. SmsMessage[] msg = null ;
  8. String msgStr = "";
  9.  
  10. if ( bundle != null ){
  11. Object[] pdus = (Object[]) bundle.get ("pdus");
  12. msg = new SmsMessage [pdus.length] ;
  13.  
  14. for( int i = 0; i<msg.length; i++){
  15.  
  16. msg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
  17. msgStr = msg[i].getDisplayOriginatingAddress();
  18.  
  19. }
  20.  
  21. Intent smsIntent =new Intent(context,MainActivity.class);
  22. smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  23. smsIntent.putExtra("msgStr",msgStr);
  24. context.startActivity(smsIntent);
  25. }
  26.  
  27. }
  28.  
  29. public class MainActivity extends AppCompatActivity {
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35.  
  36. Intent sms_intent = getIntent();
  37. Bundle b = sms_intent.getExtras();
  38.  
  39. if( b != null ){
  40. notificationCall(b.getString("sms_str"),"message");
  41. }
  42. }
  43.  
  44. public void notificationCall(String title , String message){
  45.  
  46. NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
  47. .setDefaults(NotificationCompat.DEFAULT_ALL)
  48. .setSmallIcon(R.drawable.ic_launcher_background)
  49. .setContentTitle(title+"n")
  50. .setContentText(message);
  51.  
  52. NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  53. notificationManager.notify(1,notificationBuilder.build());
  54. }
Add Comment
Please, Sign In to add comment