I have an app with widget. Three buttons on widget should sent predefined SMS messages to predefined numbers ... this is my code:
package cro.perger.bonbon;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;
public class HelloWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
String encodedHash = Uri.encode("#");
for (int appWidgetId : appWidgetIds) {
Intent callIntent1 = new Intent("android.intent.action.CALL",
Uri.parse("tel:*100" + encodedHash));
Intent callIntent2 = new Intent("android.intent.action.CALL",
Uri.parse("tel:*200*1" + encodedHash));开发者_StackOverflow社区
Intent sendIntent1 = new Intent(Intent.ACTION_VIEW);
sendIntent1.putExtra("sms_body", "Poruka 1");
sendIntent1.putExtra("address", "5556");
sendIntent1.setType("vnd.android-dir/mms-sms");
Intent sendIntent2 = new Intent(Intent.ACTION_VIEW);
sendIntent2.putExtra("sms_body", "Poruka 2");
sendIntent2.putExtra("address", "5556");
sendIntent2.setType("vnd.android-dir/mms-sms");
Intent sendIntent3 = new Intent(Intent.ACTION_VIEW);
sendIntent3.putExtra("sms_body", "Poruka 3");
sendIntent3.putExtra("address", "5556");
sendIntent3.setType("vnd.android-dir/mms-sms");
Intent openintent = new Intent(context, bonbon.class);
PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, callIntent1, 0);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, callIntent2, 0);
PendingIntent pendingIntent3 = PendingIntent.getActivity(context, 0, sendIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent4 = PendingIntent.getActivity(context, 0, sendIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent5 = PendingIntent.getActivity(context, 0, sendIntent3, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent6 = PendingIntent.getActivity(context, 0, openintent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.button1, pendingIntent1);
views.setOnClickPendingIntent(R.id.button2, pendingIntent2);
views.setOnClickPendingIntent(R.id.button3, pendingIntent3);
views.setOnClickPendingIntent(R.id.button4, pendingIntent4);
views.setOnClickPendingIntent(R.id.button5, pendingIntent5);
views.setOnClickPendingIntent(R.id.button6, pendingIntent6);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Problem is that button3, button4 and button5 (when I click on them) don't send sms, but only open new sms screen, and I need to click on send SMS. What do I need to change, that SMS automaticly sends to recipient ??
Just ripped some of my old code from an app, this should work for sending SMS messages:
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.gsm.SmsManager;
import android.util.Log;
send_text_message (Number, "This is a message");
private void send_text_message (String phoneNumber, String message)
{
if (phoneNumber.length() > 0 && message.length() > 0)
{
Log.i (TAG, "MyPhoneStateListener: onCallStateChanged(); test text sent to " + phoneNumber);
sendSMS(phoneNumber, message);
}
else
{
Log.i (TAG, "MyPhoneStateListener: onCallStateChanged(); test text was NOT sent to " + phoneNumber);
}
}
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
If you want to send a SMS message, you should check out the SmsManager
and sendTextMessage()
in the official documentation:
http://developer.android.com/reference/android/telephony/SmsManager.html
Use the SmsManager and the method sendTextMessage()
import android.app.Activity;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class EnvoieSms extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//On récupère le bouton créer en XML grâce à son id
Button btnEnvoie = (Button)findViewById(R.id.envoyer);
//On récupère les deux EditText correspondant aux champs pour entrer le numéro et le message
final EditText numero =(EditText)findViewById(R.id.numero);
final EditText message = (EditText)findViewById(R.id.message);
//On affecte un écouteur d'évènement au bouton
btnEnvoie.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
//On récupère ce qui a été entré dans les EditText
String num = numero.getText().toString();
String msg = message.getText().toString();
//Si le numéro est supérieur à 4 charactère et que le message n'est pas vide on lance la procédure d'envoi
if(num.length()>= 4 && msg.length() > 0){
//Grâce à l'objet de gestion de SMS (SmsManager) que l'on récupère grâce à la méthode static getDefault()
//On envoit le SMS à l'aide de la méthode sendTextMessage
SmsManager.getDefault().sendTextMessage(num, null, msg, null, null);
//On efface les deux EditText
numero.setText("");
message.setText("");
}else{
//On affiche un petit message d'erreur dans un Toast
Toast.makeText(EnvoieSms.this, "Enter le numero et/ou le message", Toast.LENGTH_SHORT).show();
}
}
});
}
}
精彩评论