Reputation: 303
hi there i want to start an application on receiving sms from a particular number or particular port. i am trying it with onReceiveintent but i m struggling. so, anyone there to help me in detail?Please explain me with code. Thanks
Upvotes: 0
Views: 2109
Reputation: 3689
try this...it will start camera when you received sms from the port 5556
public class MySmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private String yourNumber = "5556";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
Log.v("MySMS", intent.getAction());
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
if (messages.length > -1) {
String no = messages[0].getDisplayOriginatingAddress();
Log.v("MySMS", no);
if (no != null && no.trim().equals(yourNumber)) {
PackageManager manager = context.getPackageManager();
Intent resultIntent = new Intent();
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.setComponent(new ComponentName(
"com.android.camera",
"com.android.camera.Camera"));
ResolveInfo ri = manager.resolveActivity(resultIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
if (ri != null) {
context.startActivity(resultIntent);
}
}
}
}
}
}
}
and don't forget to add this permission in your manifest file
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
Upvotes: 1