在 Android 中显示撰写短信视图
Posted
技术标签:
【中文标题】在 Android 中显示撰写短信视图【英文标题】:Show compose SMS view in Android 【发布时间】:2011-06-25 10:39:53 【问题描述】:我想用 android 发送短信。
发送短信的目的是什么?
我想显示撰写短信视图,并在消息字段中传递我的预定义文本。
【问题讨论】:
您应该阅读this acticle 了解全文。 我在这里做类似的事情!!! ***.com/questions/14452808/… @toobsco42:不,你正在做完全相反的事情。您正在自己发送短信,而这个问题要求通过标准短信应用程序。 您也可以支持 Dual-Sim 设备,请参阅:***.com/a/30677542/2267723 这篇文章简单解释了Android上的短信/彩信,提供了发送短信和接收短信的简单代码示例:codeproject.com/Articles/1044639/… 【参考方案1】:您可以省略电话号码,让用户只从联系人中进行选择,但在正文中插入您的短信文本。代码适用于 Xamarin Android:
var uri = Uri.Parse("smsto:"); //append your number here for explicit nb
var intent = new Intent(Intent.ActionSendto, uri);
intent.PutExtra("sms_body", text);
Context.StartActivity(intent);
在哪里
上下文是Xamarin.Essentials.Platform.CurrentActivity ?? Application.Context
【讨论】:
【参考方案2】:您可以使用它向任何号码发送短信:
public void sendsms(View view)
String phoneNumber = "+880xxxxxxxxxx";
String message = "Welcome to sms";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + phoneNumber));
intent.putExtra("sms_body", message);
startActivity(intent);
【讨论】:
更新:如果使用Intent发送短信,则不需要android.permission.SEND_SMS权限。【参考方案3】:上面解释的某些内容仅用于将 SMS 置于“准备启动”状态。
正如 Senthil Mg 所说,您可以使用短信管理器直接发送短信,但 SMSManager
已移至 android.telephony.SmsManager
我知道这不是很多更多信息,但有一天它可能会对某人有所帮助。
【讨论】:
问题明确要求不自己发送短信。【参考方案4】:从 KitKat 及以上发送短信:- 在您的 AndroidManifest.xml
中添加此权限<uses-permission android:name="android.permission.SEND_SMS"/>
您还应该实现 Marshmallow 及以上版本的运行时权限。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.defaultmessanginggit">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ConversationListActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ComposeSMSActivity"
android:label="@string/title_activity_compose_sms" >
</activity>
</application>
</manifest>
代码如下:-
activity_conversation_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<Button
android:id="@+id/btn_send_msg"
android:layout_
android:layout_
android:text="Message" />
</LinearLayout>
ConversationListActivity.java
public class ConversationListActivity extends FragmentActivity
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private int PERMISSIONS_REQUEST_RECEIVE_SMS = 130;
private Button btn_send_sms;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversation_list);
btn_send_sms = (Button) findViewById(R.id.btn_send_msg);
btn_send_sms.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
int hasSendSMSPermission = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
hasSendSMSPermission = checkSelfPermission(Manifest.permission.SEND_SMS);
if (hasSendSMSPermission != PackageManager.PERMISSION_GRANTED)
requestPermissions(new String[]Manifest.permission.SEND_SMS,
PERMISSIONS_REQUEST_RECEIVE_SMS);
else if (hasSendSMSPermission == PackageManager.PERMISSION_GRANTED)
Intent intent = new Intent(ConversationListActivity.this, ComposeSMSActivity.class);
startActivity(intent);
else
Intent intent = new Intent(ConversationListActivity.this, ComposeSMSActivity.class);
startActivity(intent);
);
这是短信布局和发送短信的代码:-
activity_compose_sms.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_
android:layout_
tools:ignore="MergeRootFrame" />
</LinearLayout>
fragment_compose_sms.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<LinearLayout
android:orientation="horizontal"
android:layout_
android:layout_>
<TextView
android:layout_
android:layout_
android:text="@string/compose_to"
android:id="@+id/textView"
android:layout_gravity="center_vertical" />
<EditText
android:layout_
android:layout_
android:inputType="phone"
android:ems="10"
android:id="@+id/composeEditTextTo" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_
android:layout_>
<TextView
android:layout_
android:layout_
android:text="@string/compose_message"
android:id="@+id/textView2"
android:layout_gravity="center_vertical" />
<EditText
android:layout_
android:layout_
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/composeEditTextMessage"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_
android:layout_>
<Button
android:layout_
android:layout_
android:text="@string/compose_cancel"
android:id="@+id/composeButtonCancel" />
<Button
android:layout_
android:layout_
android:text="@string/compose_send"
android:id="@+id/composeButtonSend" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_
android:paddingTop="10dp"
android:id="@+id/composeNotDefault"
android:visibility="invisible">
<TextView
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/compose_not_default"
android:id="@id/composeNotDefault" />
<Button
android:layout_
android:layout_
android:text="@string/compose_set_default"
android:id="@+id/composeButtonSetDefault" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
ComposeSMSActivity.java
public class ComposeSMSActivity extends Activity
Activity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compose_sms);
mActivity = this;
if (savedInstanceState == null)
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
getActionBar().setDisplayHomeAsUpEnabled(true);
public static class PlaceholderFragment extends Fragment
public PlaceholderFragment()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
final View rootView = inflater.inflate(R.layout.fragment_compose_sms, container, false);
rootView.findViewById(R.id.composeButtonCancel).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
NavUtils.navigateUpTo(getActivity(), new Intent(getActivity(), ConversationListActivity.class));
);
rootView.findViewById(R.id.composeButtonSend).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String recipient = ((TextView) rootView.findViewById(R.id.composeEditTextTo)).getText().toString();
String message = ((TextView) rootView.findViewById(R.id.composeEditTextMessage)).getText().toString();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(recipient, "ME", message, null, null);
);
return rootView;
就是这样。
【讨论】:
【参考方案5】:试试下面的代码,然后拨打sendSMS("99999999999", "message");
以发送所需号码的短信。
//---sends an SMS message to another device---
@SuppressWarnings("deprecation")
private void sendSMS(String phoneNumber, String message)
Log.v("phoneNumber",phoneNumber);
Log.v("MEssage",message);
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, **DummyClasshere.class**), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
请将以下权限放入AndroidManifest.xml
文件中。
<uses-permission android:name="android.permission.SEND_SMS"/>
希望这可能会有所帮助。
更新 来自评论: DummyClasshere.class 是一项活动,无需执行任何流程或您需要在其中导航的类。
您可以使用 Object.class 代替 DummyClasshere.class。
【讨论】:
不做任何流程的活动。或者你需要导航的类.. 我只使用new Intent()
就可以了。
您如何知道是否发送了消息(短信)?【参考方案6】:
这允许通过现有应用程序发送短信。 phoneNumber - 是字符串。如果您不想指定电话号码,请使用空字符串“”。
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", phoneNumber, null));
sendIntent.putExtra("sms_body", "text message");
startActivity(sendIntent);
【讨论】:
【参考方案7】:您可以使用以下代码:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"
+ phoneNumber)));
确保将phoneNumber
设置为您要向其发送消息的电话号码
您可以使用(来自 cmets)向 SMS 添加消息:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + phoneNumber));
intent.putExtra("sms_body", message);
startActivity(intent);
【讨论】:
感谢您的代码。我想添加,因为我需要输入我的预定义文本。我得到了这个解决方案 Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "sms:" + phoneNumber ) ); intent.putExtra("sms_body", message);开始活动(意图); 如何在通讯录中设置“phoneNumber”? @djk 谢谢我想阅读所有联系人,并希望拥有自己的页面来发送自动完成的短信,但我无法阅读联系人任何好的教程? 我会使用默认的短信撰写小部件来做这件事。只需将电话号码留空(这是可选的) 嗨。我想发送群发短信。就像单击发送 10,000 条短信一样。可能吗 ?我听说安卓每 30 分钟发送 30 条短信。请帮助我【参考方案8】:希望对你有帮助...
文件名 = MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import android.view.View.OnClickListener;
import android.view.*;
public class MainActivity extends Activity implements OnClickListener
Button click;
EditText txt;
TextView txtvw;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button)findViewById(R.id.button);
txt = (EditText)findViewById(R.id.editText);
txtvw = (TextView)findViewById(R.id.textView1);
click.setOnClickListener(this);
@Override
public void onClick(View v)
txt.setText("");
v = this.getCurrentFocus();
try
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("8017891398",null,"Sent from Android",null,null);
catch(Exception e)
txtvw.setText("Message not sent!");
if(v != null)
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(),0);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
在 AndroidManifest.xml 中添加这一行
<uses-permission android:name="android.permission.SEND_SMS" />
【讨论】:
【参考方案9】:这对我有用。
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
sendSMS("5556", "Hi You got a message!");
/*here i can send message to emulator 5556. In Real device
*you can change number*/
);
//Sends an SMS message to another device
private void sendSMS(String phoneNumber, String message)
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
您可以在 AndroidManifest.xml 中添加这一行
<uses-permission android:name="android.permission.SEND_SMS"/>
看看this
这可能对你有帮助。
【讨论】:
问题要求在标准撰写视图中显示 SMS。这完全相反,在用户背后发送短信,不是吗? 如果消息中有撇号,则发送失败。【参考方案10】:如果它可以帮助某人,我会添加我的 SMS 方法。 小心 smsManager.sendTextMessage,如果文本太长,消息不会消失。您必须尊重最大长度,具体取决于编码。 更多信息在这里SMS Manager send mutlipart message when there is less than 160 characters
//无处不在
SMSUtils.sendSMS(context, phoneNumber, message);
//清单
<!-- SMS -->
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<receiver
android:name=".SMSUtils"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="SMS_SENT"/>
<action android:name="SMS_DELIVERED"/>
</intent-filter>
</receiver>
//JAVA
public class SMSUtils extends BroadcastReceiver
public static final String SENT_SMS_ACTION_NAME = "SMS_SENT";
public static final String DELIVERED_SMS_ACTION_NAME = "SMS_DELIVERED";
@Override
public void onReceive(Context context, Intent intent)
//Detect l'envoie de sms
if (intent.getAction().equals(SENT_SMS_ACTION_NAME))
switch (getResultCode())
case Activity.RESULT_OK: // Sms sent
Toast.makeText(context, context.getString(R.string.sms_send), Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: // generic failure
Toast.makeText(context, context.getString(R.string.sms_not_send), Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE: // No service
Toast.makeText(context, context.getString(R.string.sms_not_send_no_service), Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU: // null pdu
Toast.makeText(context, context.getString(R.string.sms_not_send), Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF: //Radio off
Toast.makeText(context, context.getString(R.string.sms_not_send_no_radio), Toast.LENGTH_LONG).show();
break;
//detect la reception d'un sms
else if (intent.getAction().equals(DELIVERED_SMS_ACTION_NAME))
switch (getResultCode())
case Activity.RESULT_OK:
Toast.makeText(context, context.getString(R.string.sms_receive), Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(context, context.getString(R.string.sms_not_receive), Toast.LENGTH_LONG).show();
break;
/**
* Test if device can send SMS
* @param context
* @return
*/
public static boolean canSendSMS(Context context)
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
public static void sendSMS(final Context context, String phoneNumber, String message)
if (!canSendSMS(context))
Toast.makeText(context, context.getString(R.string.cannot_send_sms), Toast.LENGTH_LONG).show();
return;
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT_SMS_ACTION_NAME), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED_SMS_ACTION_NAME), 0);
final SMSUtils smsUtils = new SMSUtils();
//register for sending and delivery
context.registerReceiver(smsUtils, new IntentFilter(SMSUtils.SENT_SMS_ACTION_NAME));
context.registerReceiver(smsUtils, new IntentFilter(DELIVERED_SMS_ACTION_NAME));
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
ArrayList<PendingIntent> sendList = new ArrayList<>();
sendList.add(sentPI);
ArrayList<PendingIntent> deliverList = new ArrayList<>();
deliverList.add(deliveredPI);
sms.sendMultipartTextMessage(phoneNumber, null, parts, sendList, deliverList);
//we unsubscribed in 10 seconds
new Handler().postDelayed(new Runnable()
@Override
public void run()
context.unregisterReceiver(smsUtils);
, 10000);
【讨论】:
【参考方案11】:希望这段代码能帮到你:)
public class MainActivity extends Activity
private int mMessageSentParts;
private int mMessageSentTotalParts;
private int mMessageSentCount;
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
String phoneNumber = "0000000000";
String message = "Hello World!";
sendSMS(phoneNumber,message);
);
public void sendSMS(String phoneNumber,String message)
SmsManager smsManager = SmsManager.getDefault();
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
int messageCount = parts.size();
Log.i("Message Count", "Message Count: " + messageCount);
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
for (int j = 0; j < messageCount; j++)
sentIntents.add(sentPI);
deliveryIntents.add(deliveredPI);
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver()
@Override
public void onReceive(Context arg0, Intent arg1)
switch (getResultCode())
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
, new IntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver()
@Override
public void onReceive(Context arg0, Intent arg1)
switch (getResultCode())
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
, new IntentFilter(DELIVERED));
smsManager.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
/* sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents); */
【讨论】:
【参考方案12】:String phoneNumber = "0123456789";
String message = "Hello World!";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
在您的 AndroidManifest.xml 文件中包含以下权限
<uses-permission android:name="android.permission.SEND_SMS" />
【讨论】:
【参考方案13】:这肯定会起作用,在此,不使用任何意图发送消息。
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("Phone Number", null, "Message", null, null);
此代码用于在后台发送消息(不显示消息编写器),它也可以在广播接收器内部工作。如果你想从广播接收器发送消息。
<uses-permission android:name="android.permission.SEND_SMS"/>
【讨论】:
+1。为我的要求工作。但我错过了一件事。此消息未出现在我发送的项目中。为什么会这样? 我们没有使用默认的 Inten 进程发送短信,我们只是使用短信管理器。 Intent 将满足维护 SmsManager 的日志。 是的。这对我非常有用。但是发送短信后给出的确认信息意味着它可能会更好。 我只是建议在 try-catch 中添加这个块。【参考方案14】:在 Android 中,我们有类 SmsManager 来管理 SMS 操作,例如发送数据、文本和 pdu SMS 消息。通过调用静态方法 SmsManager.getDefault() 获取该对象。
SmsManager Javadoc
查看以下链接获取发送短信的示例代码:
article on sending and receiving SMS messages in Android
【讨论】:
以上是关于在 Android 中显示撰写短信视图的主要内容,如果未能解决你的问题,请参考以下文章