How to Activate an Android App by a SMS from a specific SMS Sender (Specific SMS Port)
I need to develop an Android App which will receive SMS from a Specific sender, when the SMS received, the App ha开发者_运维技巧s to get activated and gets all the values which came with the SMS, please provide me the answer?
You can use BroadcastReciver for reading sms. And extract that sms and save values in DataBase in android . When you call the first Activity check the particular value contains in the DataBase then only start the 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();
}
}
}
}
}
}
This code for Reading SMS.
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();
}
}
this code for the First 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;
}
This is in DataBase class.
I think thi is helpful for u....
Use a broadcast receiver to capture all the incoming messages. However, where, when and how you initialize your receiver depends on your application. You can do it on boot, or on first open of your application etc.
You will have to scan all the incoming sms, read the content, and the number and check and set a flag somewhere inside your application.
精彩评论