使用意图从部件上的按钮点击发送短信(Using intent for sending SMS on

2019-10-30 04:06发布

好吧,这是我的小部件的代码。 我有两个按钮,被点击它们时拨打电话。 我想实现三个按钮,用于发送短信,但我不能为实现这一意图......我我主要的应用程序我用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);
        }
    }
}

Answer 1:

你有没有使用smsmanager如下:

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


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

先在的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:

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


文章来源: Using intent for sending SMS on Button click from widget