1

I want to build an android application in which when the device will booted for the first time it will detect the IMEI and other device info and also checked if there is SIM or Not.If there is a SIM it will send a sms containing the IMEI and other device Info to a specific number.

I am new to android development and I am so much confused that how to do that.But I have to do that.Please friend help with sample code...

Thanks in advance

1 Answers1

1

First, you have to add permissions to start at bootup, get device id and send sms in AndroidManifest.xml. (More: receive bootup, permission, read imei, send sms)

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SEND_SMS" />

Then you have to add broadcast receiver component to your application in AndroidManifest.xml.

<receiver android:name=".BootReceiverMessageSender">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <category android:name="android.intent.category.NONE" />
    </intent-filter>
</receiver>

After that, you need to create the java class.

class BootReceiverMessageSender extends BroadcastReceiver {
    private static final String DESTINATION_NUMBER="+..."; /* phone number */
    @Override
    public void onReceive(Context c, Intent i) {
        TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE); /* get a TelephonyManager instance */
        String deviceId = tm.getDeviceId(); /* get the id of device (gsm: imei, cdma: meid / esn) */
        if(deviceId != null) { /* check if not null */
            SmsManager smsManager = SmsManager.getDefault(); /* get a SmsManager instance */
            smsManager.sendTextMessage(DESTINATION_NUMBER, null, "My ID: " + deviceID, null, null); /* Send SMS */
        }
    }
}

It's the easiest, but not the best way to do it!

uno20001
  • 211
  • 1
  • 5
  • But if the device has dual SIM then how can I get the IMEI of those SIM Slot??Any Idea??And also Is there no need of Background Service?? –  Jun 28 '15 at 18:33
  • [Here](http://stackoverflow.com/questions/14517338/android-check-whether-the-phone-is-dual-sim). Or use Android 5.1 API if that's the target OS version. – uno20001 Jun 28 '15 at 18:43
  • what about the background service??Is it necessary or not??I think the necessity of the Background service is that the service will send a sms and after sending the sms the service will be off.How to implement that. By the way thanks for your easiest reply.I am trying it. –  Jun 28 '15 at 18:47
  • I think it's only a plus component and it only increases the complexity. Now it's juts starts at bootup and send the message, why would be a background service needed? – uno20001 Jun 28 '15 at 19:25
  • If the device is failed to send the sms when booted??Then I think a service will try to send the sms again.What is your opinion?? –  Jun 29 '15 at 03:11
  • I have tried your way but it did not send the sms.Where is the problem??Does it require any other things to do?? –  Jun 30 '15 at 17:26
  • Have you checked everything? Is phone number correct? Have you put log messages in the code? You can try to use [pendingintents](http://bit.ly/19dm2VG) to check if the message was successfully sent. – uno20001 Jun 30 '15 at 21:55
  • I have never work with pending intents.Is there any example to do that??Can you give me a example of pending intents ??How can I bind pending intents with the above code??Please tell me .... –  Jul 01 '15 at 03:43
  • Sorry for my late reply. So you can create a new BroadcastReceiver which will be started when the sms was sent. PedingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent().setClass(context, SMSSentBroadcastReceiver.class), 0); and you can pass it to smsManager.sendTextMessage(). (I didn't test it) – uno20001 Jul 03 '15 at 18:57