Last active
June 22, 2025 14:29
-
-
Save sunmeat/67e2433b92a1cf09d143be3da95c60e6 to your computer and use it in GitHub Desktop.
sms receiver android example
This file contains hidden or 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
MainActivity.java: | |
package com.alex.sms; | |
import android.Manifest; | |
import android.content.IntentFilter; | |
import android.content.pm.PackageManager; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.core.app.ActivityCompat; | |
import androidx.core.content.ContextCompat; | |
public class MainActivity extends AppCompatActivity { | |
private static final int PERMISSION_REQUEST_CODE = 1; | |
private SmsReceiver callReceiver; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
checkAndRequestPermissions(); | |
callReceiver = new SmsReceiver(); | |
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); | |
registerReceiver(callReceiver, filter, RECEIVER_EXPORTED); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
if (callReceiver != null) { | |
unregisterReceiver(callReceiver); | |
} | |
} | |
private void checkAndRequestPermissions() { | |
String[] permissions = { | |
Manifest.permission.RECEIVE_SMS, | |
Manifest.permission.READ_SMS | |
}; | |
boolean allPermissionsGranted = true; | |
for (String permission : permissions) { | |
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { | |
allPermissionsGranted = false; | |
break; | |
} | |
} | |
if (!allPermissionsGranted) { | |
ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE); | |
} else { | |
Toast.makeText(this, "Permissions already granted!", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
if (requestCode == PERMISSION_REQUEST_CODE) { | |
boolean allGranted = true; | |
for (int result : grantResults) { | |
if (result != PackageManager.PERMISSION_GRANTED) { | |
allGranted = false; | |
break; | |
} | |
} | |
if (allGranted) { | |
Toast.makeText(this, "Permissions granted!", Toast.LENGTH_SHORT).show(); | |
} else { | |
Toast.makeText(this, "Some permissions were denied!", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
SmsReceiver.java: | |
package com.alex.sms; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.telephony.SmsMessage; | |
import android.widget.Toast; | |
public class SmsReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if (intent != null && "android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())) { | |
var bundle = intent.getExtras(); | |
if (bundle != null) { | |
try { | |
var pdus = (Object[]) bundle.get("pdus"); | |
String format = bundle.getString("format"); // Получаем формат PDU | |
if (pdus != null) { | |
for (Object pdu : pdus) { | |
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdu, format); // Используем format | |
String senderNumber = currentMessage.getDisplayOriginatingAddress(); | |
String message = currentMessage.getDisplayMessageBody(); | |
Toast.makeText(context, "Sender: " + senderNumber + "; Message: " + message, Toast.LENGTH_LONG).show(); | |
} | |
} | |
} catch (Exception e) { | |
Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
activity_main.xml: | |
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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" | |
android:background="#E6FFCC" | |
android:gravity="center" | |
android:orientation="vertical" | |
android:padding="16dp"> | |
<Button | |
android:id="@+id/callButton" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:backgroundTint="#FF4081" | |
android:elevation="6dp" | |
android:foreground="?android:attr/selectableItemBackground" | |
android:paddingStart="24dp" | |
android:paddingTop="12dp" | |
android:paddingEnd="24dp" | |
android:paddingBottom="12dp" | |
android:text="Ожидайте прихода СМС" | |
android:textColor="#FFFFFF" | |
android:textSize="18sp" | |
android:textStyle="bold" | |
tools:ignore="HardcodedText" /> | |
</LinearLayout> | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
AndroidManifest.xml: | |
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<uses-permission android:name="android.permission.RECEIVE_SMS" /> | |
<uses-permission android:name="android.permission.READ_SMS" /> | |
<uses-permission android:name="android.permission.SEND_SMS" /> | |
<uses-feature | |
android:name="android.hardware.telephony" | |
android:required="false" /> | |
<application | |
android:allowBackup="true" | |
android:dataExtractionRules="@xml/data_extraction_rules" | |
android:fullBackupContent="@xml/backup_rules" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/Theme.Files" | |
tools:targetApi="35"> | |
<receiver | |
android:name=".SmsReceiver" | |
android:enabled="true" | |
android:exported="true" | |
android:permission="android.permission.BROADCAST_SMS"> | |
<intent-filter> | |
<action android:name="android.provider.Telephony.SMS_RECEIVED" /> | |
</intent-filter> | |
</receiver> | |
<activity | |
android:name=".MainActivity" | |
android:exported="true"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment