安卓短信意图过滤器

5
我在我的Android应用程序中尝试了这段代码用于短信消息,但它没有起作用,该应用程序未出现在消息列表中。我需要添加一些内容才能使它工作吗?
             <action android:name="android.intent.action.SENDTO" />
               <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="sms" />
            <data android:scheme="smsto" />
                <data android:mimeType="text/plain" />

          </intent-filter>

你在清单文件中设置了权限吗?android.permission.SEND_SMS 权限。 - Jav_Rock
你尝试过android.intent.action.SEND(而不是SENDTO)吗? - njzk2
请参考我在下面链接中的回答:http://stackoverflow.com/questions/11677784/android-register-application-to-send-compose-sms/24068732#24068732 - gopalanrc
2个回答

11

我为您提供一份详细的说明,以在不同情况下(包括联系人、文本共享等)完成此操作。

您的消息活动清单条目

<!-- Defines also the app name in the Android menu -->
    <activity
    android:name="it.rainbowbreeze.smsforfree.ui.ActSendSms"
    android:label="@string/common_appName"
    >
    <!-- Sends sms for someone  -->
    <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.SENDTO" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="sms" />
    <data android:scheme="smsto" />
    </intent-filter>

    <!-- Sends text to someone .This will enable any Text Share functionality-->
    <intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
    </intent-filter>
    </activity>

现在我们已经创建了一个名为processIntentData的方法,如下所示,该方法适用于消息活动:

private void processIntentData(Intent intent)
{
    if (null == intent) return;

    if (Intent.ACTION_SENDTO.equals(intent.getAction())) {
        //in the data i'll find the number of the destination
        String destionationNumber = intent.getDataString();
        destionationNumber = URLDecoder.decode(destionationNumber);
        //clear the string
        destionationNumber = destionationNumber.replace("-", "")
            .replace("smsto:", "")
            .replace("sms:", "");
        //and set fields
        mTxtDestination.setText(destionationNumber);

    } else if (Intent.ACTION_SEND.equals(intent.getAction()) && "text/plain".equals(intent.getType())) {
        //in the data i'll find the content of the message
        String message = intent.getStringExtra(Intent.EXTRA_TEXT);
        //clear the string
        mTxtBody.setText(message);
    }
}

按照消息活动中所示的使用方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    mTxtDestination = (EditText) findViewById(R.id.actsendsms_txtDestination);
    mTxtBody = (EditText) findViewById(R.id.actsendsms_txtMessage);

    ...

    //executed when the application first runs
    if (null == savedInstanceState) {
        processIntentData(getIntent());
    }
}
附上结果的截图: 在此输入图片描述

0

尝试使用以下代码发送短信,在您的活动清单文件中授予android.permission.SEND_SMS权限

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Enter Phone Number:"
   />
<EditText 
   android:id="@+id/smsnumber"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:inputType="phone"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Enter Phone SMS Text:"
   />
<EditText 
   android:id="@+id/smstext"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<Button 
   android:id="@+id/sendsms"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text=" Send SMS "
   />
<Button 
   android:id="@+id/sendsms_intent"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text=" Send SMS using Intent.ACTION_SENDTO "
   />
</LinearLayout>

现在Activity类是AndroidSMS.java

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidSMS extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       final EditText edittextSmsNumber = (EditText)findViewById(R.id.smsnumber);
       final EditText edittextSmsText = (EditText)findViewById(R.id.smstext);
       Button buttonSendSms = (Button)findViewById(R.id.sendsms);
       Button buttonSendSms_intent = (Button)findViewById(R.id.sendsms_intent);

       buttonSendSms.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    SmsManager smsManager = SmsManager.getDefault();
    String smsNumber = edittextSmsNumber.getText().toString();
    String smsText = edittextSmsText.getText().toString();
    smsManager.sendTextMessage(smsNumber, null, smsText, null, null);
   }});

       buttonSendSms_intent.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub

    String smsNumber = edittextSmsNumber.getText().toString();
    String smsText = edittextSmsText.getText().toString();

    Uri uri = Uri.parse("smsto:" + smsNumber);
    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
    intent.putExtra("sms_body", smsText);  
    startActivity(intent);
   }});
   }
}

谢谢你的代码,但我想解决清单文件问题,并使应用程序看起来像这样http://i.stack.imgur.com/cif2V.png。 - steevoo
@steevoo:你的意思是,当你点击一个按钮或者点击应用程序布局中的任何一个时,你必须展示链接中提到的内容,我理解的对吗? - Aerrow
不需要点击按钮,当用户想要发送短信时,它会自动出现,就像WhatsApp和Skype等应用程序一样。 - steevoo
<action android:name="android.intent.action.SENDTO" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="sms" /> <data android:scheme="smsto" /> </intent-filter> 我用以下方式解决了这个问题。 - steevoo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接