Advertisement
GGGG2468

SMS

Jan 30th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. activity_main.xml
  2.  
  3. <?xml version="1.0" encoding="utf-8"?>
  4. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  xmlns:tools="http://schemas.android.com/tools"
  6.  android:layout_width="match_parent"
  7.  android:layout_height="match_parent"
  8.  tools:context=".MainActivity"
  9.  tools:ignore="Deprecated">
  10.  <TextView
  11.  android:id="@+id/idNumber"
  12.  android:layout_width="120dp"
  13.  android:layout_height="wrap_content"
  14.  android:layout_x="16dp"
  15.  android:layout_y="16dp"
  16.  android:hint="Sender Phone No"/>
  17.  <TextView
  18.  android:id="@+id/idMessage"
  19.  android:layout_width="120dp"
  20.  android:layout_height="wrap_content"
  21.  android:layout_x="16dp"
  22.  android:layout_y="35dp"
  23.  android:hint="Message Content"/>
  24. </AbsoluteLayout>
  25.  
  26. MainActivity.java
  27.  
  28.  
  29. package com.example.program6;
  30. import android.os.Bundle;
  31. import android.widget.TextView;
  32. import androidx.appcompat.app.AppCompatActivity;
  33. public class MainActivity extends AppCompatActivity {
  34.  TextView number, message;
  35.  @Override
  36.  protected void onCreate(Bundle b) {
  37.  super.onCreate(b);
  38.  setContentView(R.layout.activity_main);
  39.  number = findViewById(R.id.idNumber);
  40.  message = findViewById(R.id.idMessage);
  41.  Bundle b = getIntent().getBundleExtra("data");
  42.  if(b != null){
  43.  number.setText(b.getString("number"));
  44.  message.setText(b.getString("message"));
  45.  }
  46.  }
  47. }
  48.  
  49. MyReciever.java
  50.  
  51.  
  52. package com.example.program6;
  53. import android.os.Bundle;
  54. import android.content.Intent;
  55. import android.content.Context;
  56. import android.telephony.SmsMessage;
  57. import android.content.BroadcastReceiver;
  58. public class MyReceiver extends BroadcastReceiver {
  59.  @Override
  60.  public void onReceive(Context context, Intent intent) {
  61.  Object [] objmessages = (Object[]) intent.getExtras().get("pdus");
  62.  for(int i = 0; i < objmessages.length; i++){
  63.  SmsMessage sms = SmsMessage.createFromPdu((byte[]) objmessages[i]);
  64.  Bundle b = new Bundle();
  65.  b.putString("num", sms.getOriginatingAddress());
  66.  b.putString("msg", sms.getMessageBody());
  67.  Intent it = new Intent(context, MainActivity.class);
  68.  it.putExtra("data", b);
  69.  it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  70.  context.startActivity(it);
  71.  }
  72.  }
  73. }
  74.  
  75.  
  76. AndroidManifest.xml
  77.  
  78. <uses-permission android:name="android.permission.RECEIVE_SMS"/>
  79.  
  80.  
  81. <receiver
  82.  android:name="com.example.program6.MyReceiver"
  83.  android:exported="true">
  84.  <intent-filter>
  85.  <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  86.  </intent-filter>
  87. </receiver>
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement