gpt4 book ai didi

java - 如何接收来自特定号码的短信?

转载 作者:行者123 更新时间:2023-11-29 23:18:13 25 4
gpt4 key购买 nike

我想接收来自特定号码的消息。当我运行该应用程序时,我的收件箱中会收到来自每个人的消息。我从this site (android authority)获取了源码

 package com.nqr.smsapp;

import android.Manifest; import android.content.ContentResolver;
import android.content.pm.PackageManager; import
android.database.Cursor; import android.net.Uri; import
android.support.annotation.NonNull; import
android.support.v4.content.ContextCompat; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.telephony.SmsManager; import android.view.View; import
android.widget.ArrayAdapter; import android.widget.EditText; import
android.widget.ListView; import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

ArrayList<String> smsMessagesList = new ArrayList<>();
ListView messages;
ArrayAdapter arrayAdapter;
EditText input;
SmsManager smsManager = SmsManager.getDefault();
private static MainActivity inst;

private static final int READ_SMS_PERMISSIONS_REQUEST = 1;

public static MainActivity instance() {
return inst;
}

@Override
public void onStart() {
super.onStart();
inst = this;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messages = (ListView) findViewById(R.id.messages);
input = (EditText) findViewById(R.id.input);
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, smsMessagesList);
messages.setAdapter(arrayAdapter);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
getPermissionToReadSMS();
} else {
refreshSmsInbox();
}


}

public void updateInbox(final String smsMessage) {
arrayAdapter.insert(smsMessage, 0);
arrayAdapter.notifyDataSetChanged();
}

public void onSendClick(View view) {

if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
getPermissionToReadSMS();
} else {
smsManager.sendTextMessage("07911127456", null, input.getText().toString(), null, null);
Toast.makeText(this, "Message sent!", Toast.LENGTH_SHORT).show();
} }

public void getPermissionToReadSMS() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_SMS)) {
Toast.makeText(this, "Please allow permission!", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{Manifest.permission.READ_SMS},
READ_SMS_PERMISSIONS_REQUEST);
}
}


@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults) {
// Make sure it's our original READ_CONTACTS request
if (requestCode == READ_SMS_PERMISSIONS_REQUEST) {
if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Read SMS permission granted", Toast.LENGTH_SHORT).show();
refreshSmsInbox();
} else {
Toast.makeText(this, "Read SMS permission denied", Toast.LENGTH_SHORT).show();
}

} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}



}

public void refreshSmsInbox() {
ContentResolver contentResolver = getContentResolver();
Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null,
null, null);
int indexBody = smsInboxCursor.getColumnIndex("body");
int indexAddress = smsInboxCursor.getColumnIndex("address");
if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
arrayAdapter.clear();
do {
String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
"\n" + smsInboxCursor.getString(indexBody) + "\n";
arrayAdapter.add(str);
} while (smsInboxCursor.moveToNext()); //messages.setSelection(arrayAdapter.getCount() - 1);
}



}
>

我希望消息从特定号码发送到我的收件箱:例如我的收件箱中只有来自 07911 127456 的消息

我只需要一个特定的对话

广播接收器

package com.nqr.smsapp;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SmsBroadcastReceiver extends BroadcastReceiver {

public static final String SMS_BUNDLE = "pdus";

public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();

if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) {
String format = intentExtras.getString("format");
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format);

String smsBody = smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();

smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}

MainActivity inst = MainActivity.instance();
inst.updateInbox(smsMessageStr);
}
}
}

最佳答案

基于本教程 site(android authority)

在你的广播接收器中

int yourNumber=1234567890; // your specific number

String address = smsMessage.getDisplayOriginatingAddress(); // incoming SMS number

if(address ==yourNumber){

//display message
}

更新

public class SmsBroadcastReceiver extends BroadcastReceiver {
String address;
public static final String SMS_BUNDLE = "pdus";

public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();

if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) {
String format = intentExtras.getString("format");
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format);

String smsBody = smsMessage.getMessageBody().toString();
address = smsMessage.getOriginatingAddress();


smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";



}
if(address =="7911127456"){
MainActivity inst = MainActivity.instance();
inst.updateInbox(smsMessageStr);
}
}
}
}

更新

   public void refreshSmsInbox() {
ContentResolver contentResolver = getContentResolver();
Cursor smsInboxCursor =
contentResolver.query(Uri.parse("content://sms/inbox"), null, null,
null, null);
int indexBody = smsInboxCursor.getColumnIndex("body");
int indexAddress = smsInboxCursor.getColumnIndex("address");
if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
arrayAdapter.clear();

do {
if(indexAddress ==7911127456){
String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
"\n" + smsInboxCursor.getString(indexBody) + "\n";
arrayAdapter.add(str);
}
} while (smsInboxCursor.moveToNext());

//messages.setSelection(arrayAdapter.getCount() - 1);
arrayAdapter.notifyDataSetChanged();
}

关于java - 如何接收来自特定号码的短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54904761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com