我有一个需要用户名和密码的android应用程序,我的服务器通过短信向用户发送用户名和密码。Android应用程序从SMS获取用户名和密码
我正在寻找一种方式,通过它我的应用程序可以读取短信,并且可以在设置中自动使用用户名和密码进行自我配置,而不是在设置中手动配置它!
我有一个需要用户名和密码的android应用程序,我的服务器通过短信向用户发送用户名和密码。Android应用程序从SMS获取用户名和密码
我正在寻找一种方式,通过它我的应用程序可以读取短信,并且可以在设置中自动使用用户名和密码进行自我配置,而不是在设置中手动配置它!
AndroidManifest.xml中
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<receiver android:name=".Receiver" >
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
Receiver.java
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
public class Receiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
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]);
checkSMS(context, msgs[i]);
}
}
}
void checkSMS(Context context, SmsMessage sms)
{
String msg = sms.getMessageBody().toString();
}
}
使用具有高priorty和rcv的SMS广播监听器,检查它是否来自您的服务器用于发送SMS的号码,如果是,则从SMS文本中提取信息并相应更新您的应用的配置,然后您可以甚至使用abortBroad铸造方法,如果您想要的短信不应该在本地短信应用中显示
嗨,能否请您提供一些示例代码,即俯视图? – AAB 2012-04-12 21:01:09
这里是最简单的代码接收短信http://androidsourcecode.blogspot.com/2010/10/receiving-sms-using-broadcastreceiver.html和这个链接也有很好的细节http://boomtech.in/entries/general/ sms-toast - using-broadcast-receiver --- retrive-contact-information – 2012-04-12 21:08:27
我把这个叫做如下,它只执行一次,并且不会在20秒或更长时间内保持活动。消息将在20秒内发送我希望它应该持续听至少20秒。 \t'sms.sendTextMessage(“XXXXXX”,null,“MSG”,null,null); Receiver rc = new Receiver(); rc.onReceive(getContext(),new Intent(“android.provider.Telephony.SMS_RECEIVED”));' – AAB 2012-04-16 09:26:28
我不明白你的意思,但我认为你做错了什么,你不需要创建新的Receiver类并调用rc.onreceive。创建一个Receiver.java作为我给你的样本,并添加你的清单这个类,就像我在我的答案中写的那样。每当手机从某人手中获取短信时,android电话框架将调用您的应用程序Receiver类OnReceive方法即使您的应用程序未打开。你可以在检查功能上做任何你想做的事情。 – 2012-04-16 16:04:40