【问题标题】:Using intent for sending SMS on Button click from widget使用意图在从小部件单击按钮时发送 SMS
【发布时间】:2011-08-09 12:01:07
【问题描述】:

好的,这是我的小部件的代码。我有两个按钮,点击它们时会打电话。我想再实现三个按钮来发送短信,但我无法实现它的意图......我的主应用程序我使用 smsmanager 功能......

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

【问题讨论】:

    标签: android button click widget


    【解决方案1】:

    你有没有如下使用过短信管理器:

    SmsManager sm = SmsManager.getDefault();
    sm.sendTextMessage(destinationAddress, null, "Hello world", null, null, null);
    


    除了您的代码,我建议您覆盖 WidgetProvider 中的 onReceive() 方法以处理发送 SMS。基本实现可能如下所示:

    首先在onUpdate()中:

    Intent intent = new Intent(context, WidgetProvider.class);
    intent.setAction(ACTION_SEND_SMS);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    

    然后:

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

    在清单中:

    <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>
    


    希望有帮助

    已编辑:

    首先定义消息列表。有很多方法——在这个例子中,你可以将它们存储在字符串数组中:

    String[] messages = new String[]{"Message for button 1", "Message for button 2", "Message for button 3"};
    String number = "12344444454" // recipient's mobile number
    

    初始化短信管理器:

    SmsManager sm = SmsManager.getDefault();
    

    现在为您的按钮添加 onClick 监听器:

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

    【讨论】:

    • 我正在使用 SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, null, null); ;但是这个 tu 变量带有下划线并显示错误“无法解析为变量”而且我需要向同一个号码发送三个不同的消息,并且消息是预定义的,而不是用户输入的??
    • 是的,但我需要发送取决于按下按钮的消息,我有三个消息按钮,一个消息是一个按钮,而不是同时发送所有三个消息:)跨度>
    • Tnahks。对不起,但我有新问题,我需要把这段代码放在哪里? (在属于小部件活动的 xml 中,或者在我的应用程序主要活动的 xml 中??)
    • 这取决于你的按钮在哪里
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多