检查Android中的短信顺序
我正在通过安卓检查Android中的短信顺序,android,smsmanager,Android,Smsmanager,我正在通过安卓smsmsmanager发送5条消息,需要检查送货单。在订单中发送的SMS是否以相同的订单交付。如果没有,交货顺序是什么 我想我可以通过BroadcastReceiver来检查发货情况,但是订单呢 谢谢在res/layout文件夹中的main.xml文件中,添加以下代码,以便用户可以输入电话号码和要发送的消息: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://sc
smsmsmanager
发送5条消息,需要检查送货单。在订单中发送的SMS是否以相同的订单交付。如果没有,交货顺序是什么
我想我可以通过BroadcastReceiver
来检查发货情况,但是订单呢
谢谢在res/layout文件夹中的main.xml文件中,添加以下代码,以便用户可以输入电话号码和要发送的消息:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the phone number of recipient"
/>
<EditText
android:id="@+id/txtPhoneNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Message"
/>
<EditText
android:id="@+id/txtMessage"
android:layout_width="fill_parent"
android:layout_height="150px"
android:gravity="top"
/>
<Button
android:id="@+id/btnSendSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send SMS"
/>
</LinearLayout>
sendSMS()函数的定义如下:
public class SMS extends Activity
{
//...
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
//...
}
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, SMS.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
}
}
要发送SMS消息,请使用SmsManager类。与其他类不同,您不直接实例化这个类;相反,您将调用getDefault()静态方法来获取SmsManager对象。sendTextMessage()方法发送带有PendingEvent的SMS消息。PendingEvent对象用于标识稍后要调用的目标。例如,发送消息后,可以使用PendingEvent对象显示另一个活动。在本例中,PendingEvent对象(pi)只是指向同一个活动(SMS.java),因此当发送SMS时,不会发生任何事情
如果需要监视SMS消息发送过程的状态,实际上可以将两个PendingEvent对象与两个BroadcastReceiver对象一起使用,如下所示
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
除了以编程方式发送SMS消息外,还可以使用BroadcastReceiver对象截获传入的SMS消息
要查看如何从Android应用程序中接收SMS消息,请在AndroidManifest.xml文件中添加元素,以便SmsReceiver类可以拦截传入的SMS消息:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.SMSMessaging"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SMS"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>
在SmsReceiver类中,扩展BroadcastReceiver类并重写onReceive()方法:
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.gsm.sms消息;
导入android.widget.Toast;
公共类SmsReceiver扩展了BroadcastReceiver
{
@凌驾
公共void onReceive(上下文、意图)
{
//---获取传入的SMS消息---
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
对于(int i=0;i“是否是按相同顺序交付的订单发送的SMS”--如果您在知道第一个订单已收到之前发送第二个,我不会指望这一点。“如果不是,那么交付顺序是什么?”--移动运营商想要什么样的订单。@Commonware谢谢,我只想知道订单。我不想控制订单。我不能在短信上附加一个ID之类的东西,我可以在deliverBroadcastReceiver
中检查它吗?@MustafaMohammadi,如果要在发送邮件时得到通知,您将向短信管理员提供一个挂起的内容SmsManager的end方法。当您从系统收到回拨时,您可以在内部检查发送顺序。我面前没有要检查的设备,但如果您检查了要返回的目的,它将包含信息,您可以将其映射到发送的短信。谢谢,但我知道发送和接收短信的概念,我需要一个unique解决方案。请再次阅读我的问题。我正在给广播接收器检查短信发送或不发送,您也可以找到短信订单和广播接收器。请仔细阅读这是什么?检查订单的部分。当短信发送时,广播释放器中提到特定操作,您可以通过订单手动查找计算
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.SMSMessaging"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SMS"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}