SMS-App - Receiver und Toasts

  • 5 Antworten
  • Letztes Antwortdatum
A

Andrawit

Neues Mitglied
0
Moin zusammen,

bin gerade dabei mir eine kleine App zu basteln, die auf Knopfdruck festgelegte Texte (Befehle) an eine vorher eingestellte Nummer schickt. Da ich noch nicht so fortgeschritten bin in der Android-App-Entwicklung habe ich mir erstmal folgenden Code aus dem Netz gesucht:

Code:
package com.example.smssender;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity 
{
   Button btnSendSMS;
   EditText txtPhoneNo;
   EditText txtMessage;
   /* Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);        
       btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
       txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
       txtMessage = (EditText) findViewById(R.id.txtMessage);
       btnSendSMS.setOnClickListener(new View.OnClickListener() 
       {
           public void onClick(View v) 
           {                
               String phoneNo = txtPhoneNo.getText().toString();
               String message = txtMessage.getText().toString();                 
               if (phoneNo.length()>0 && message.length()>0)                
                   sendSMS(phoneNo, message);                
               else
                   Toast.makeText(getBaseContext(), 
                       "Please enter both phone number and message.", 
                       Toast.LENGTH_SHORT).show();
           }
       });        
   }    
   
   //---sends an SMS message to another device---
   private void sendSMS(String phoneNumber, String message)
   {        
       String SENT = "SMS_SENT";
       String DELIVERED = "SMS_DELIVERED";
       PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
           new Intent(SENT), 0);
       PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
           new Intent(DELIVERED), 0);
       //---when the SMS has been sent---
       registerReceiver(new BroadcastReceiver()
       {
  @Override
           public void onReceive(Context arg0, Intent arg1) 
           {
               switch (getResultCode())
               {
                   case Activity.RESULT_OK:
                       Toast.makeText(getBaseContext(), "SMS sent", 
                               Toast.LENGTH_SHORT).show();
                       break;
                   case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                       Toast.makeText(getBaseContext(), "Generic failure", 
                               Toast.LENGTH_SHORT).show();
                       break;
                   case SmsManager.RESULT_ERROR_NO_SERVICE:
                       Toast.makeText(getBaseContext(), "No service", 
                               Toast.LENGTH_SHORT).show();
                       break;
                   case SmsManager.RESULT_ERROR_NULL_PDU:
                       Toast.makeText(getBaseContext(), "Null PDU", 
                               Toast.LENGTH_SHORT).show();
                       break;
                   case SmsManager.RESULT_ERROR_RADIO_OFF:
                       Toast.makeText(getBaseContext(), "Radio off", 
                               Toast.LENGTH_SHORT).show();
                       break;
               }
           }  
       }, new IntentFilter(SENT));
       //---when the SMS has been delivered---
       registerReceiver(new BroadcastReceiver()
       {
           @Override
           public void onReceive(Context arg0, Intent arg1) 
           {
               switch (getResultCode())
               {
                   case Activity.RESULT_OK:
                       Toast.makeText(getBaseContext(), "SMS delivered", 
                               Toast.LENGTH_SHORT).show();
                       break;
                   case Activity.RESULT_CANCELED:
                       Toast.makeText(getBaseContext(), "SMS not delivered", 
                               Toast.LENGTH_SHORT).show();
                       break;                        
               }
           }
       },       new IntentFilter(DELIVERED));        
       SmsManager sms = SmsManager.getDefault();
       sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
   }
}

Der Code ist im Grunde schon eine komplette App mit zwei Textfeldern - einmal für die Rufnummer und einmal für den Text. Habe mir die App auch schon aufs Handy gezogen um es zu testen. Funktioniert soweit super, bis auf eine kleine Sache. Sobald die App versendet werden konnte oder sobald ein Fehler vorliegt und sie nicht versendet werden konnte, wird das in einem Toast angezeigt. Das ganze ist in diesem Code-Abschnitt umgesetzt worden:

Code:
[SIZE=2]
[LEFT]registerReceiver([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] BroadcastReceiver()
{

[/SIZE][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onReceive(Context arg0, Intent arg1) 
{
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]switch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (getResultCode())
{
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Activity.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_OK[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SMS sent"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 
Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).show();
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SmsManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_ERROR_GENERIC_FAILURE[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Generic failure"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 
Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).show();
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SmsManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_ERROR_NO_SERVICE[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"No service"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 
Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).show();
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SmsManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_ERROR_NULL_PDU[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Null PDU"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 
Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).show();
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SmsManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_ERROR_RADIO_OFF[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Radio off"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 
Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).show();
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
}
} 
}, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] IntentFilter(SENT));

[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---when the SMS has been delivered---[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]registerReceiver([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] BroadcastReceiver()
{
[/SIZE][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onReceive(Context arg0, Intent arg1) 
{
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]switch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (getResultCode())
{
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Activity.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_OK[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SMS delivered"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 
Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).show();
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Activity.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_CANCELED[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SMS not delivered"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 
Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).show();
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; 
}
}[/LEFT]
}, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] IntentFilter(DELIVERED));
[/SIZE]

Es ist allerdings jetzt so, dass jedes mal der vorherige Toast wieder mit aufgerufen wird. Heißt also: Ich sende zum ersten mal eine SMS dann erscheint 1x "SMS sent". Sende ich zum zweiten mal eine SMS erscheint 2x ein Toast mit "SMS sent". Beim dritten mal, ... usw.

Ich habe eine grobe Vermutung, dass es mit dem Receiver zusammenhängt und man diesen irgendwie beenden muss. Ist das richtig? Oder woran liegt es?

LG
 
Weiß niemand woran es liegen könnte?
 
Das Problem ist vermutlich, dass du den Reciever jedes mal neu hinzufügst. bei jedem senden rufst du register Reciever auf.

Du hast beim 2. Aufruf also 2 Reciever angehängt. dau die Reciever auch jedes mal neuerstellst, also ein neues Objekt baust, hast du dann 2 Reciever dran die natürlich beide ausgeführt werden.
Theoretisch kann man ja mehr als 1 Reciever, die dann was anderes tun anhängen, du hängst aber jedesmal den gleichen dran, der dann natürlich auch mehrfach ausgeführt wird.

Du solltest das register Reciever irekt in die onCreate Methode verschieben und nicht beim senden vom SMS jedesmal wieder hinzufügen.
 
Hi,

danke für die Antwort - klingt soweit logisch. Wie muss ich den Code denn dann abändern? Habe es gerade schon versucht aber irgendwie kriege ich das nicht hin - an irgendwas meckert er immer rum.
 
Wenn ich das richtig sehe sollte es reichen wenn du:

registerReceiver(new BroadcastReceiver() etc......)

vor deinem onclicklistener erledigst:
Also vor zeile
btnSendSMS.setOnClickListener(new View.OnClickListener()

Dann sollte der reciever jeweils nur einmal hinzugefügt werden.

Also alles was in deiner sendSMS methode raus was nach
//---when the SMS has been sent---
kommt.
Das sollte funktionieren.

Woran meckert er denn rum?

Du musst noch
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
die beiden zeilen am besten in die Klasse selber packen

am besten direkt über
Button btnSendSMS;
 
Ahhh!

Super, vielen Dank! ;) Funktioniert einwandfrei. Dachte eigentlich, dass ich es auch so gemacht habe - aber irgendwo war ein Fehler. Warum auch immer.

Dann kann ich das jetzt so in meine App einfügen und das Projekt beenden :thumbsup:

Dankeee
 

Ähnliche Themen

G
Antworten
0
Aufrufe
132
Gerdchen07
G
G
Antworten
1
Aufrufe
384
Gerdchen07
G
G
Antworten
13
Aufrufe
598
Gerdchen07
G
L
Antworten
2
Aufrufe
554
Lexub
L
migi01
Antworten
26
Aufrufe
1.990
migi01
migi01
Zurück
Oben Unten