如何通过SMS从特定的SMS发件人(特定SMS端口)激活Android应用程序

时间:2011-08-04 03:25:01

标签: android ssms

我需要开发一个Android应用程序,它将接收来自特定发件人的短信,当收到短信时,应用程序必须被激活并获得短信附带的所有值,请给我答案?

2 个答案:

答案 0 :(得分:1)

使用广播接收器捕获所有传入的消息。但是,初始化接收器的位置,时间和方式取决于您的应用。您可以在启动时或首次打开应用程序等时执行此操作。

您必须扫描所有传入的短信,阅读内容和数字,然后检查并在应用程序内的某处设置标记。

答案 1 :(得分:1)

您可以使用BroadcastReciver读取短信。并在android中的DataBase中提取该短信并保存值。当您调用第一个Activity检查DataBase中包含的特定值时,只启动Activity。

 public class ReadingMessage extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras(); 
    DBAdapter dbHelper = new DBAdapter(context);
    SmsMessage[] msgs = null;
    String msg=null;
    String str=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]);                

            msg = msgs[i].getMessageBody().toString();
            str =msg.toUpperCase();        


            if(str.contains("your value"))
            {
            try{
                dbHelper.open();

                dbHelper.insertinfo(msg);                   

                dbHelper.close();

            }
            catch(Exception e)
            {
                e.toString();
            }

            }

        }
           }  
  }

}

此代码用于阅读短信。

 public class StartActivity extends Activity{

    private static final int ACTIVITY_REGISTRATION1=0;
    private static final int ACTIVITY_SENDALERT3=1;
    private static final int ACTIVITY_REGISTRATION2 = 2;

      Context context;
      DBAdapter dbHelper=null;
      Intent intent;
      String db_activation=null;
      Cursor cursor;

  public StartActivity()
  {
      this.context=this;
  }

@Override

/* Method Header
 * Method Name      : onCreate
 * Input Parameter  : Bundle
 * Return Value     : nil
 */
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

     dbHelper=new DBAdapter(this);

     try
     {
     dbHelper.open();

     cursor = dbHelper.getActivtaion();
     if(cursor.getCount()==0)
     {

        intent=new Intent(this,Registration.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        startActivityForResult(intent,ACTIVITY_REGISTRATION1);
     }
     else
     {
        for(int i=0;i<cursor.getCount();i++)
        {
            cursor.moveToNext();
            db_activation = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_ACTIVATION));

     if(db_activation.equals("1"))

     {


        intent=new Intent(this,SendAlert.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        startActivityForResult(intent,ACTIVITY_SENDALERT3);



     }
    else
    {

        intent=new Intent(this,Registration.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        startActivityForResult(intent,ACTIVITY_REGISTRATION2);
    }

     dbHelper.close();
}
     }
     }
catch(Exception e)
{
finish();
System.exit(0);
   e.toString();
}
}
@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    finish();

}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
     if (resultCode == Activity.RESULT_OK)
     finish();
    }
  }

第一个活动的代码

  public long insertTruckinfo(String db_Truckmsg)
    {
    ContentValues cVal=new ContentValues();

    cVal.put(KEY_INFO,db_Truckmsg);


    return db.insert(TRUCKINFO_TABLE, null,cVal);

}


public Cursor getActivtaion()
{
     Cursor cursor =db.query(ACTIVATION_TABLE, new String[] {KEY_ID,KEY_ACTIVATION}, null,null, null, null, null);
     return cursor;
}


public Cursor getTruckinfo()
{
     Cursor cursor =db.query(TRUCKINFO_TABLE, new String[] {KEY_ID,KEY_INFO}, null,null, null, null, null);
     return cursor;
}

这是在DataBase类中。

我认为这对你有帮助....