问题
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 for that... I my main app I use smsmanager function ....
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) {
//Timer timer = new Timer();
//timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
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));
PendingIntent pendingIntent1 =
PendingIntent.getActivity(context, 0, callIntent1, 0);
PendingIntent pendingIntent2 =
PendingIntent.getActivity(context, 0, callIntent2, 0);
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views1 = new RemoteViews(context.getPackageName(), R.layout.widget);
views1.setOnClickPendingIntent(R.id.button1, pendingIntent1);
RemoteViews views2 = new RemoteViews(context.getPackageName(), R.layout.widget);
views2.setOnClickPendingIntent(R.id.button2, pendingIntent2);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views1);
appWidgetManager.updateAppWidget(appWidgetId, views2);
}
}
}
回答1:
Have you used the smsmanager as follows:
SmsManager sm = 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():
Intent intent = new Intent(context, WidgetProvider.class);
intent.setAction(ACTION_SEND_SMS);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
And then:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(ACTION_SEND_SMS)) {
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(destinationAddress, null, "Hello world", null, null, null);
}
}
And in the Manifest:
<receiver android:name="com.packagename.WidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.packagename.ACTION_SEND_SMS"/>
</intent-filter>
<meta-data android: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 = new String[]{"Message for button 1", "Message for button 2", "Message for button 3"};
String number = "12344444454" // recipient's mobile number
Initialize SmsManager:
SmsManager sm = 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(new OnClickListener(){
@Override
public void onClick(View v) {
//Sending message 1
sm.sendTextMessage(number, null, messages[0], null, null, null);
}
});
button2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//Sending message 2
sm.sendTextMessage(number, null, messages[1], null, null, null);
}
});
button3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//Sending message 3
sm.sendTextMessage(number, null, messages[2], null, null, null);
}
});
来源:https://stackoverflow.com/questions/6995861/using-intent-for-sending-sms-on-button-click-from-widget