我正在制作一个关于短信活动的应用程序,该应用程序是关于如果手机在振动或静音模式下丢失在家中某处,用户可以使用特定关键字(即“丢失”)从任何其他手机发送短信。分钟电话收到此短信,触发意图并更改为振铃模式。
问问题
1696 次
2 回答
0
您可以在 SMS Reciverd 上开始您的活动:
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 += msgs[i].getMessageBody().toString();
str += "\n";
}
if(str.contains("YOUR_UNIQUE_KEY"))
{
// START YOUR ACTIVITY HERE
Intent i = new Intent(context,SMSActivity.class);
i.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
}
对于我们如何在 ANDROID 中接收短信,请参阅以下教程:
http://mobiforge.com/developing/story/sms-messaging-android
http://www.anddev.org/recognize-react_on_incoming_sms-t295.html
于 2012-06-19T05:40:45.103 回答
0
在您的项目中注册 BroadcastReceiver 类监听传入的短信教程和源代码下面的链接
http://www.androidcompetencycenter.com/2008/12/android-api-sms-handling/
广播接收器类监听接收短信后启动活动示例代码如下:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class ReceiveSMS extends BroadcastReceiver
{
MapMarker obj_gps = new MapMarker();
Boolean SendSMS;
String Mobileno;
String VarMessageBody;
@Override
public void onReceive(Context context, Intent intent)
{
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();
Mobileno=msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
VarMessageBody=msgs[i].getMessageBody().toString();
str += "\n";
Mobileno=msgs[i].getOriginatingAddress();
}
//---display the new SMS message---
if (VarMessageBody.startsWith("LOST"))//Check Message Body String Start with START
Intent intentHome = new Intent(context, HomeScreen.class);
intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intentHome.putExtra("No",Mobileno);
//intentHome.putExtra("Msg",VarMessageBody);
//intentHome.putExtra("Mob", Mobileno);
context.startActivity(intentHome);
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
此代码接收发送短信格式“丢失”开始活动..!
于 2012-06-19T05:42:36.897 回答