2012-01-13 60 views
3

我在我的android应用程序中为SMS消息尝试了这段代码,但它不起作用,应用程序没有出现在消息列表中。我应该添加一些东西,使其工作?Android sms intent过滤器

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

您是否在清单中设置了权限? android.permission.SEND_SMS许可 – 2012-01-13 08:49:43

+0

做你试过android.intent.action.SEND(不SENDTO?) – njzk2 2012-01-13 09:18:37

+0

:请参考链接我的回答如下 http://stackoverflow.com/questions/11677784/android-register -application-to-send-compose-sms/24068732#24068732 – gopalanrc 2014-06-05 19:37:50

回答

0

试试这个代码发送短信,在您的活动manifiest文件隆重android.permission.SEND_SMS permission

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

thx代码,但我想解决清单,并使应用程序出现像这样http://i.stack.imgur.com/cif2V.png – steevoo 2012-01-13 08:56:48

+0

@steevoo :你的意思是,当你点击一个按钮或点击你应用程序布局中的任何一个时,你必须显示你在链接中提到的是哪一个,我是否正确? – Aerrow 2012-01-13 09:02:13

+0

没有点击按钮它出现时,用户希望发送短信作为WhatsApp和Skype和那些应用程序 – steevoo 2012-01-13 09:25:21

8

我提供你详细的说明去做,在不同的情况下(与通讯录,文字股等)。

清单条目为您的邮件活动

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

附加的结果捕捉: enter image description here