【问题标题】:SMS Messaging not sending短信不发送
【发布时间】:2016-02-19 07:31:48
【问题描述】:

我一直在关注一个视频,一切都在编译,但是当我点击发送时,toast 永远不会出现,消息也永远不会出现在我的手机上。我还在 Android Manifest 中添加了 SEND_SMS 权限。有什么建议吗?

SendSmsActivity.java





package android_auto.engine;

        import android.app.Activity;
        import android.net.Uri;
        import android.os.Bundle;
        import android.telephony.SmsManager;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.Toast;

        import com.google.android.gms.appindexing.Action;
        import com.google.android.gms.appindexing.AppIndex;
        import com.google.android.gms.common.api.GoogleApiClient;

        import android_auto.R;

public class SendSmsActivity extends Activity {

    Button sendSMSBtn;
    EditText toPhoneNumberET;
    EditText smsMessageET;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_activity);
        sendSMSBtn = (Button) findViewById(R.id.sendSMSBtn);
        toPhoneNumberET = (EditText) findViewById(R.id.toPhoneNumberET);
        smsMessageET = (EditText) findViewById(R.id.smsMessageET);
        sendSMSBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                sendSMS();
            }
        });
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    protected void sendSMS() {
        String toPhoneNumber = toPhoneNumberET.getText().toString();
        String smsMessage = smsMessageET.getText().toString();
        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(toPhoneNumber, null, smsMessage, null, null);
            Toast.makeText(getApplicationContext(), "SMS sent.",
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    "Sending SMS failed.",
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "SendSms Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://cen4910.ucf.edu.aaa_android_auto.engine/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "SendSms Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://cen4910.ucf.edu.aaa_android_auto.engine/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}

sms_activity.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/toPhoneNumberTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Phone Number" />

    <EditText
        android:id="@+id/toPhoneNumberET"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"/>

    <TextView
        android:id="@+id/smsMessageTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SMS Message" />

    <EditText
        android:id="@+id/smsMessageET"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"/>

    <Button android:id="@+id/sendSMSBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send SMS"/>

</LinearLayout>

Android 清单

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android_auto">

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


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <activity
            android:name=".engine.SendSmsActivity"
            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=".engine.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.android.gms.car.application"
            android:resource="@xml/automotive_app_desc" />
        <!--
 Main music service, provides media browsing and media playback services to
         consumers through MediaBrowserService and MediaSession. Consumers connect to it through
         MediaBrowser (for browsing) and MediaController (for playback control)
        -->
        <service
            android:name=".MyMusicService"
            android:exported="true">
            <intent-filter>
                <action android:name="android.media.browse.MediaBrowserService" />
            </intent-filter>
        </service>

        <service android:name=".MyMessagingService"></service>

        <receiver android:name=".MessageReadReceiver">
            <intent-filter>
                <action android:name="com.example.myapplication.ACTION_MESSAGE_READ" />
            </intent-filter>
        </receiver>
        <receiver android:name=".MessageReplyReceiver">
            <intent-filter>
                <action android:name="com.example.myapplication.ACTION_MESSAGE_REPLY" />
            </intent-filter>
        </receiver>
        <!-- ATTENTION: This was auto-generated to add Google Play services to your project for
             App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

【问题讨论】:

  • 您在发件人收件箱中看到了吗? adb 抛出什么错误?
  • 吐司没有出现是个问题。你能设置一个断点,看看执行是否进入了 sendSms() 方法吗?
  • 我根据提供的链接更新了我的代码,但我仍然遇到同样的问题。有什么想法吗?

标签: java android


【解决方案1】:

我在您的 Manifest 中没有看到您的 SendSmsActivity 是否正常?

【讨论】:

  • 我不太确定。这对我来说是新的。你会如何在 android manifest 中写?
  • 这就像你的 MainActivity。实际上,如果没有完成,您的应用应该会崩溃...
  • 我在这篇文章中添加了您提供的代码并更新了 Android 清单。使用该代码,我收到错误“无法解析符号“SendSmsActivity”有什么想法吗?
  • 尝试添加“.engine”。在活动名称之前
  • 我现在可以识别它,但是当我运行时,点击发送按钮后我仍然没有任何响应
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-24
  • 2017-06-22
相关资源
最近更新 更多