gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:21:58 28 4
gpt4 key购买 nike

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

最佳答案

您可以使用 BroadcastReciver 来阅读短信。并提取该短信并将值保存在 android 的数据库中。当您调用第一个 Activity 时,检查数据库中包含的特定值,然后只启动 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();
}
}

第一个 Activity 的代码

  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;
}

这是在数据库类中。

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

关于android - 如何通过来自特定 SMS 发件人(特定 SMS 端口)的 SMS 激活 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6935850/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com