短信文字不发送

时间:2016-02-24 15:15:58

标签: java android android-pendingintent smsmanager

我在发送短信时遇到问题。当我点击按钮时,没有任何反应。没有消息或任何东西。 没有意义的是,它作为短信发送的独立应用程序,但如果我将代码集成到我的大型项目中,它不会发送短信。什么都没发生。 有些东西必须与Smsmanager或其他东西发生冲突。任何人都可以告诉我为什么它不再起作用了吗?

Android Manifest

export const routes = (
  <Route component={Application} name="application">
    <Route component={Home} path="/"/>
    <Route component={Login} path="/login"/>
    <Route component={requireAuthentication(Dashboard)} path="/dashboard"/>
    <Route component={requireAuthentication(Locations)} path="/locations"/>
    <Route component={requireAuthentication(EditLocationTemplate)} path="/locations/template/location"/>
    <Route component={NotFound} path="*" />
  </Route>
);

SendSmsActivity.java

<?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" />
    <uses-feature android:glEsVersion="0x00020000" android:required="true" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <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.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>

        <activity android:name=".ui.MapsActivity1">

        </activity>

        <meta-data android:name="com.google.android.gms.car.application" android:resource="@xml/automotive_app_desc" />
        <meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyCMFYfJ6aOuxJk3W0vmhF6Nou3TP_qIU6c" />

        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <!--
     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" />

        <receiver android:name=".MessageReadReceiver">
            <intent-filter>
                <action android:name="com.example.zachboone.myapplication.ACTION_MESSAGE_READ" />
            </intent-filter>
        </receiver>
        <receiver android:name=".MessageReplyReceiver">
            <intent-filter>
                <action android:name="com.example.zachboone.myapplication.ACTION_MESSAGE_REPLY" />
            </intent-filter>
        </receiver>

    </application>




</manifest>

activity_sms.xml

package android_auto.engine;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import android_auto.R;


public class SendSmsActivity extends Activity {

    Button buttonSend;
    EditText textPhoneNo;
    EditText textSMS;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);

        buttonSend = (Button) findViewById(R.id.buttonSend);
        textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
        textSMS = (EditText) findViewById(R.id.editTextSMS);

        buttonSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String phoneNo = textPhoneNo.getText().toString();
                String sms = textSMS.getText().toString();

                try {
                    SmsManager smsManager = SmsManager.getDefault();
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(SendSmsActivity.this, 0, new Intent("SMS_SENT"), 0);


                    smsManager.sendTextMessage(phoneNo, null, sms, pendingIntent, null);
                    Toast.makeText(getApplicationContext(), "SMS Sent!",
                            Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(),
                            "SMS faild, please try again later!",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }

            }
        });
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试在点击监听器中编辑代码

PendingIntent pendingIntent = PendingIntent.getBroadcast(SendSmsActivity.this, 0,new Intent("SMS_SENT"), 0);
smsManager.sendTextMessage(phoneNo, null, sms, pendingIntent, null);

希望这可以帮助您实现目标。