在android中接收短信的活动

时间:2012-06-19 05:36:13

标签: android

我正在申请短信活动,应用程序是关于如果手机在振动或静音模式下丢失在某处,用户可以从任何其他具有特定关键字的电话发送短信,即“丢失”。分钟手机收到这个短信,一个意图被触发,它变为振铃模式。

2 个答案:

答案 0 :(得分:0)

您可以在收到的短信上开始您的活动:

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

答案 1 :(得分:0)

在你的Project中注册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();
            }
    }



}

此代码接收发送短信格式“丢失”以启动活动..!