使用意向来自小部件按钮点击发送短信

编程入门 行业动态 更新时间:2024-10-27 19:17:58
本文介绍了使用意向来自小部件按钮点击发送短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

确定这是code为我的小部件。我有两个按钮,被点击它们时拨打电话。我想实现三个按钮发送短信,但我不能为实现这一意图......我我主要的应用程序我用smsmanager功能....

进口android.app.PendingIntent;进口android.appwidget.AppWidgetManager;进口android.appwidget.AppWidgetProvider;进口android.content.Context;进口android.content.Intent;进口android.Uri;进口android.widget.RemoteViews;公共类HelloWidget的扩展AppWidgetProvider {    @覆盖    公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,            INT [] appWidgetIds){        //定时器定时器=新的Timer();        //timer.scheduleAtFixedRate(new数值指明MyTime(上下文,appWidgetManager),1,1000);        字符串连接codedHash = Uri.en code(#);        对于(INT appWidgetId:appWidgetIds){            意图callIntent1 =新意图(android.intent.action.CALL            Uri.parse(电话:* 100+ EN codedHash));            意图callIntent2 =新意图(android.intent.action.CALL                Uri.parse(电话:* 200 * 1+ EN codedHash));            的PendingIntent pendingIntent1 =                PendingIntent.getActivity(上下文,0,callIntent1,0);            的PendingIntent pendingIntent2 =                PendingIntent.getActivity(上下文,0,callIntent2,0);            //获取为App插件的布局并附加上单击监听器按钮            RemoteViews 0点=新的RemoteViews(context.getPackageName(),R.layout.widget);            views1.setOnClickPendingIntent(R.id.button1,pendingIntent1);            RemoteViews views2 =新的RemoteViews(context.getPackageName(),R.layout.widget);            views2.setOnClickPendingIntent(R.id.button2,pendingIntent2);            //告诉AppWidgetManager对当前应用的Widget进行更新            appWidgetManager.updateAppWidget(appWidgetId,0点);            appWidgetManager.updateAppWidget(appWidgetId,views2);        }    }}

解决方案

你有没有使用smsmanager如下:

SmsManager SM = SmsManager.getDefault();sm.sendTextMessage(destinationAddress,NULL,世界,你好,NULL,NULL,NULL);

除了你的code,我建议您覆盖的onReceive在WidgetProvider()方法以处理发送短信。基本实现看起来是这样的:

先在的onUpdate():

意向意图=新意图(背景下,WidgetProvider.class);intent.setAction(ACTION_SEND_SMS);的PendingIntent的PendingIntent = PendingIntent.getBroadcast(上下文,0,意图,0);

和则:

@覆盖公共无效的onReceive(上下文的背景下,意图意图){    super.onReceive(背景下,意图);    如果(intent.getAction()。等于(ACTION_SEND_SMS)){        SmsManager SM = SmsManager.getDefault();        sm.sendTextMessage(destinationAddress,NULL,世界,你好,NULL,NULL,NULL);    }}

和在清单:

<接收机器人:名字=com.packagename.WidgetProvider>    &所述;意图滤光器>        <作用机器人:名字=android.appwidget.action.APPWIDGET_UPDATE/>        <作用机器人:名字=com.packagename.ACTION_SEND_SMS/>    &所述; /意图滤光器>    <元数据机器人:名字=android.appwidget.provider               机器人:资源=@ XML / widget_info/>< /接收器>

希望帮助

编辑:

首先定义邮件列表。有许多方法 - 在这个例子中你可以将它们存储在字符串数组中:

的String []消息=新的String [] {消息按钮1,消息按钮2,消息按钮3};串号=12344444454//收件人的移动电话号码

初​​始化SmsManager:

SmsManager SM = SmsManager.getDefault();

现在添加的onClick监听到你的按钮:

按钮按钮1 =(按钮)findViewById(R.id.yourbutton1);按钮按钮2 =(按钮)findViewById(R.id.yourbutton2);按钮按钮3 =(按钮)findViewById(R.id.yourbutton3);button1.setOnClickListener(新OnClickListener(){    @覆盖    公共无效的onClick(视图v){        //发送消息1        sm.sendTextMessage(数字,空,消息[0],​​NULL,NULL,NULL);    }});button2.setOnClickListener(新OnClickListener(){    @覆盖    公共无效的onClick(视图v){        //发送消息2        sm.sendTextMessage(数字,空,消息[1],NULL,NULL,NULL);    }});button3.setOnClickListener(新OnClickListener(){    @覆盖    公共无效的onClick(视图v){        //发送消息3        sm.sendTextMessage(数字,空,消息[2],NULL,NULL,NULL);    }});

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.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); } } }

解决方案

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); } });

更多推荐

使用意向来自小部件按钮点击发送短信

本文发布于:2023-06-09 04:09:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/594937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送短信   部件   意向   按钮

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!