0

I created a Backgroundservice where i want to send SMS in the Background when i press the power button for example.

This is my code for sending sms when i press the power button:

public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
            SmsManager smsManager = SmsManager.getDefault();
 smsManager.sendTextMessage(phoneNo, null, "Ich brauche Hilfe! Hier ist mein aktueller Standort, kannst du mir helfen kommen?" + "\n" + "http://maps.google.com/?q=" + lat + "," + lon, null, null);
    return true;
       }
    return super.onKeyDown(keyCode, event);

What i want to ask is: Do i need to write this code into my Backgroundervice and start the Service in the MainActivty onKeyDown? or keep it in my MainActivity?

bfmv991
  • 109
  • 1
  • 12
  • Possible duplicate of [Sends SMS in Background when Power Button is pressed](http://stackoverflow.com/questions/33569625/sends-sms-in-background-when-power-button-is-pressed) – fejd Nov 11 '15 at 23:09
  • This is not actually a duplicate. The question here is "ok, but having created a service to send messages in the background, does this code fragment block the UI thread? I think. – Robin Davies Nov 12 '15 at 06:28
  • YOu can run this code on the UI thread, no problem. The method follows an Async pattern already based on the last two arguments to the method. – Robin Davies Nov 12 '15 at 06:34

0 Answers0