【问题标题】:Set up method for sending messages设置发送消息的方法
【发布时间】:2013-10-29 05:40:17
【问题描述】:

我正在尝试使用 android eclipse 创建 SMS 文本消息。我是android eclipse的初学者。我需要帮助来创建发送消息的方法。我无法在 2 个模拟器之间发送消息。有人可以帮我解决这个方法吗?谢谢!

主要:

package com.example.sms;

import com.example.sms.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
Button sendSMS;
EditText msgTxt;
EditText numTxt;
IntentFilter intentFilter;


private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent)
{
    TextView inTxt = (TextView) findViewById(R.id.textMsg);
    inTxt.setText(intent.getExtras().getString("sms"));
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");

sendSMS = (Button) findViewById(R.id.sendBtn);
msgTxt = (EditText) findViewById(R.id.message);
numTxt = (EditText) findViewById(R.id.numberTxt);
sendSMS.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        String myMsg = msgTxt.getText().toString();
        String theNumber = numTxt.getText().toString();

    }
});
}
protected void sendMSG(String theNumber, String myMsg) {
String SENT = "Message Sent";
String DELIVERED = "Message Delivered";

 PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
 PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

 registerReceiver(new BroadcastReceiver()
 {
 public void onReceive(Context arg0, Intent arg1)
 {
    switch(getResultCode())
    {
    case Activity.RESULT_OK:
 Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_LONG).show();
        break;
    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
 Toast.makeText(getBaseContext(), "Generic Failure",Toast.LENGTH_LONG).show();
        break;
    case SmsManager.RESULT_ERROR_NO_SERVICE:
 Toast.makeText(getBaseContext(), "No Service", Toast.LENGTH_LONG).show();
        break;      
    }
 }  
  }, new IntentFilter(SENT));

 registerReceiver(new BroadcastReceiver()
 {
 public void onReceive(Context arg0, Intent arg1)
 {
    switch(getResultCode())
    {
    case Activity.RESULT_OK:
 Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_LONG).show();
        break;
    case Activity.RESULT_CANCELED:
 Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_LONG).show();
        break;  
    }
 }
 }, new IntentFilter(DELIVERED));

 SmsManager sms = SmsManager.getDefault();
 sms.sendTextMessage(theNumber, null, myMsg, sentPI, deliveredPI);
 }

 protected void onResume(){
 registerReceiver(intentReceiver, intentFilter);
 super.onResume();
 }
protected void onPause(){
unregisterReceiver(intentReceiver);
super.onPause();    
}   
}

【问题讨论】:

    标签: android eclipse methods


    【解决方案1】:

    使用这个发送消息

    SmsManager sm = SmsManager.getDefault();
    sm.sendTextMessage("1555215554", null, "message", null, null);
    

    数字应为字符串格式。您可以在设置>关于手机>状态中看到模拟器的数量。

    如果你对http://developer.android.com/reference/android/telephony/gsm/SmsManager.html#sendTextMessage(java.lang.String, java.lang.String, java.lang.String, android.app.PendingIntent, android.app.PendingIntent)感到困惑,你可以看看这个文档

    【讨论】:

    • 我无法搜索模拟器的编号。有没有其他方法可以获取模拟器的编号?
    • 看看模拟器上面写着5554或5556等等,对吧。模拟器的数量是这样的15555215554只需在模拟器顶部的of155521添加数字即可
    【解决方案2】:

    您可以使用以下方式发送:

     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"
                        + phoneNumber))); 
    

    要在 2 个模拟器之间发送,您可以使用地址栏中显示的数字: 通常它看起来像这个 5554,所以只需用它替换电话号码。

    实际上有几种发送短信的方法,其中一种是使用上述调用 内置短信应用程序,或者你可以创建自己的,现在如果你想使用上面的,只需将它放在你的 onClick() 实现中,它就会调用内置短信应用程序,

    但是,对于其他方法,请查看 THIS nice and easy example

    不要忘记在您的 Manifest.xml 中添加以下权限

    <uses-permission android:name="android.permission.SEND_SMS" />
    

    【讨论】:

    • 谢谢。我必须在特定行之后插入此代码吗?
    • 它不起作用:(我的代码出错了。我已经插入了发送和接收的权限。
    • 它看起来像这样吗? sendSMS.setOnClickListener(startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + phoneNumber)));) {
    • 像这样调用内置短信应用程序是的,但是尝试下载上面链接中的示例并运行它们,它应该可以正常工作
    • 谢谢。我正在使用短信管理器。我需要特定代码来创建方法吗?
    猜你喜欢
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多