2012-01-13 55 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