无法达到发送短信的工作意图
好的,我将首先粘贴我的代码:
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.telephony.SmsManager;
import android.widget.RemoteViews;
public class HelloWidget extends AppWidgetProvider {
private static final String ACTION_SEND_SMS = null;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
String encodedHash = Uri.encode("#");
for (int appWidgetId : appWidgetIds) {
Intent smsIntent = new Intent(context, HelloWidget.class);
smsIntent.setAction(ACTION_SEND_SMS);
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));
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);
PendingIntent pendingIntent7 = PendingIntent.getBroadcast(context, 0, smsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
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);
}
}
}
这是我的小部件的代码。 6 个按钮... 2 个是呼叫号码 .... 3 个发送短信(至少应该是这样),还有一个用于打开应用程序的按钮。呼叫按钮工作得很好,打开应用程序的按钮也很好用,但我就是找不到发送短信的按钮。此代码只能打开短信创建页面,我需要单击发送按钮,但仅此而已。请我需要帮助,有人可以告诉我如何改变我的例子,以使其发挥作用。我尝试了短信管理器,但我无法让它工作......所以请帮助我。预先感谢大家。
OK i will paste my code first :
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.telephony.SmsManager;
import android.widget.RemoteViews;
public class HelloWidget extends AppWidgetProvider {
private static final String ACTION_SEND_SMS = null;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
String encodedHash = Uri.encode("#");
for (int appWidgetId : appWidgetIds) {
Intent smsIntent = new Intent(context, HelloWidget.class);
smsIntent.setAction(ACTION_SEND_SMS);
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));
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);
PendingIntent pendingIntent7 = PendingIntent.getBroadcast(context, 0, smsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
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);
}
}
}
This code for my widget. 6 buttons ... 2 are calling numbers .... 3 sends SMS (at least they are suposed to), and one button for opening application. Buttons for calling are working great, and button for opening application too, but I just Can't get buttons to send SMS. This code only get to open SMS create page, and I need to click on send button, but nothing more. Please I need help, of someone who can show me how to change my examplet, for this to work. I tried sms manager, but I can't get this to work... So please help me. Thank you all in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,确保您的应用程序具有发送短信的正确权限(
android.permission.SEND_SMS
)然后您绝对应该使用SmsManager 实例,如 本教程。
这个问题似乎几乎相同:
First, make sure your application has the right permissions to send SMS messages (
android.permission.SEND_SMS
)Then you definitely should use a SmsManager instance as explained in this tutorial.
This questions seem to be almost the same: