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:
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:
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
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