Using Intent For Sending Sms On Button Click From Widget
Ok this is code for my widget. I have two buttons, that are making calls when clicking on them. I want to implement three more buttons for sending SMS, but i can't implement intent
Solution 1:
Have you used the smsmanager as follows:
SmsManagersm= SmsManager.getDefault();
sm.sendTextMessage(destinationAddress, null, "Hello world", null, null, null);
In addition to your code I would suggest you to override onReceive() method in the WidgetProvider in order to handle sending SMS. The basic implementation could look like this:
First in onUpdate():
Intentintent=newIntent(context, WidgetProvider.class);
intent.setAction(ACTION_SEND_SMS);
PendingIntentpendingIntent= PendingIntent.getBroadcast(context, 0, intent, 0);
And then:
@OverridepublicvoidonReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(ACTION_SEND_SMS)) {
SmsManagersm= SmsManager.getDefault();
sm.sendTextMessage(destinationAddress, null, "Hello world", null, null, null);
}
}
And in the Manifest:
<receiverandroid:name="com.packagename.WidgetProvider" ><intent-filter><actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE" /><actionandroid:name="com.packagename.ACTION_SEND_SMS"/></intent-filter><meta-dataandroid:name="android.appwidget.provider"android:resource="@xml/widget_info" /></receiver>
Hope that helps
edited:
First define list of messages. There are many ways - in this example you can store them in the string array:
String[] messages = newString[]{"Message for button 1", "Message for button 2", "Message for button 3"};
Stringnumber = "12344444454"// recipient's mobile number
Initialize SmsManager:
SmsManagersm= SmsManager.getDefault();
Now add onClick listener to your buttons:
Button button1 = (Button)findViewById(R.id.yourbutton1);
Button button2 = (Button)findViewById(R.id.yourbutton2);
Button button3 = (Button)findViewById(R.id.yourbutton3);
button1.setOnClickListener(newOnClickListener(){
@OverridepublicvoidonClick(View v) {
//Sending message 1
sm.sendTextMessage(number, null, messages[0], null, null, null);
}
});
button2.setOnClickListener(newOnClickListener(){
@OverridepublicvoidonClick(View v) {
//Sending message 2
sm.sendTextMessage(number, null, messages[1], null, null, null);
}
});
button3.setOnClickListener(newOnClickListener(){
@OverridepublicvoidonClick(View v) {
//Sending message 3
sm.sendTextMessage(number, null, messages[2], null, null, null);
}
});
Post a Comment for "Using Intent For Sending Sms On Button Click From Widget"