我在我的Android应用程序中尝试使用此代码来处理SMS消息,但它无法正常工作,应用程序不会出现在消息列表中。我应该添加一些东西来使其有效吗?
<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 :(得分:10)
我正在为您提供详细的说明,以便在不同情况下(使用联系人,文本共享等)。
您的邮件活动的清单条目
<!-- 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方法应用于Message Activity:
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());
}
}
附加的快照结果:
答案 1 :(得分:0)
尝试使用此代码发送短信,在您的活动清单文件grand 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类是Android SMS.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);
}});
}
}