Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- tools:ignore="Deprecated">
- <TextView
- android:id="@+id/idNumber"
- android:layout_width="120dp"
- android:layout_height="wrap_content"
- android:layout_x="16dp"
- android:layout_y="16dp"
- android:hint="Sender Phone No"/>
- <TextView
- android:id="@+id/idMessage"
- android:layout_width="120dp"
- android:layout_height="wrap_content"
- android:layout_x="16dp"
- android:layout_y="35dp"
- android:hint="Message Content"/>
- </AbsoluteLayout>
- MainActivity.java
- package com.example.program6;
- import android.os.Bundle;
- import android.widget.TextView;
- import androidx.appcompat.app.AppCompatActivity;
- public class MainActivity extends AppCompatActivity {
- TextView number, message;
- @Override
- protected void onCreate(Bundle b) {
- super.onCreate(b);
- setContentView(R.layout.activity_main);
- number = findViewById(R.id.idNumber);
- message = findViewById(R.id.idMessage);
- Bundle b = getIntent().getBundleExtra("data");
- if(b != null){
- number.setText(b.getString("number"));
- message.setText(b.getString("message"));
- }
- }
- }
- MyReciever.java
- package com.example.program6;
- import android.os.Bundle;
- import android.content.Intent;
- import android.content.Context;
- import android.telephony.SmsMessage;
- import android.content.BroadcastReceiver;
- public class MyReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- Object [] objmessages = (Object[]) intent.getExtras().get("pdus");
- for(int i = 0; i < objmessages.length; i++){
- SmsMessage sms = SmsMessage.createFromPdu((byte[]) objmessages[i]);
- Bundle b = new Bundle();
- b.putString("num", sms.getOriginatingAddress());
- b.putString("msg", sms.getMessageBody());
- Intent it = new Intent(context, MainActivity.class);
- it.putExtra("data", b);
- it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(it);
- }
- }
- }
- AndroidManifest.xml
- <uses-permission android:name="android.permission.RECEIVE_SMS"/>
- <receiver
- android:name="com.example.program6.MyReceiver"
- android:exported="true">
- <intent-filter>
- <action android:name="android.provider.Telephony.SMS_RECEIVED" />
- </intent-filter>
- </receiver>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement