大家好。
我想制作一个自动回复收到的短信的应用程序。 例如。
Jon Doe 向我发送 - “嗨”,应用程序获取消息正文,用我的数据库进行检查,其中我有一个潜在的响应:
ID |文字|潜在的答案 01 |你好 |你好吗?
并且应用程序发送潜在响应。
到目前为止我所取得的成就 - 应用程序接收消息,使用数据库检查它(使用“%”)并获取正确的“潜在答案”列并将其作为消息文本正文传递,但要发送它,我使用按钮。
我的接收器是一个单独的文件类
public class MyReceiver extends BroadcastReceiver {
public static String textSmsbody="";
private static final String TAG=MyReceiver.class.getSimpleName();
public static final String pdu_type="pdus";
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceive(Context context, Intent intent) {
// Get the SMS message.
Bundle bundle = intent.getExtras();
SmsMessage[] msgs;
String strMessage = "";
String format = bundle.getString("format");
// Retrieve the SMS message received.
Object[] pdus = (Object[]) bundle.get(pdu_type);
if (pdus != null) {
// Check the Android version.
boolean isVersionM =
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M);
// Fill the msgs array.
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
// Check Android version and use appropriate createFromPdu.
if (isVersionM) {
// If Android version M or newer:
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
} else {
// If Android version L or older:
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
// Build the message to show.
String a=msgs[i].getMessageBody();
textSmsbody=msgs[i].getMessageBody();
if (a.contains("?")) {
strMessage=msgs[i].getOriginatingAddress();
// strMessage += " :" + msgs[i].getMessageBody() + "\n";
}
else {
strMessage=a;
// strMessage += "SMS from" + msgs[i].getOriginatingAddress();
// strMessage += "ELSE:" + msgs[i].getMessageBody() + "\n";
}
// Log and display the SMS message.
Log.d(TAG, "onReceive: " + strMessage);
Toast.makeText(context, strMessage, Toast.LENGTH_LONG).show();
}
}
}
}
发送方法在我的MainActivity中。
public void smsSendMessage(View view) {
databaseSearch();
// Set the destination phone number to the string in editText.
String destinationAddress = "2020";
// Find the sms_message view.
// Get the text of the SMS message.
String smsMessage = sendingText;
// Set the service center address if needed, otherwise null.
String scAddress = null;
// Set pending intents to broadcast
// when message sent and when delivered, or set to null.
PendingIntent sentIntent = null, deliveryIntent = null;
// Use SmsManager.
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage
(destinationAddress, scAddress, smsMessage,
sentIntent, deliveryIntent);
}
在布局中,我有一个调用 smsSendMessage () 的按钮;
我的问题是如何在没有按钮的情况下自动完成它。
当手机收到消息时,应用程序会与数据库进行检查并自行发送。
请告诉我您是否需要查看我的 list 文件或数据库帮助程序。
最佳答案
使用 JobService 应该是适合您情况的选择。
创建一个像这样的 JobService 类
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class ExampleJobService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
//send a message
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
return true;
}
}
同时在 list 中声明
<service
android:name=".ExampleJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
现在在您的接收器中,您可以像这样启动它
ComponentName componentName = new ComponentName(context, ExampleJobService.class);
PersistableBundle bundle = new PersistableBundle();
bundle.putLong("lat", lat);
bundle.putLong("lon", lon);
JobInfo jobInfo = new JobInfo.Builder(0, componentName)
.setExtras(bundle)
.build();
有关 JobServices 的更多详细信息 https://www.vogella.com/tutorials/AndroidTaskScheduling/article.html
关于java - 自动回复广播接收器收到的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61057698/