2016-02-24 4 views
0

У меня возникли проблемы с отправкой sms-текста. Когда я нажимаю кнопку, ничего не происходит. Нет сообщений или что-то еще. Что не имеет смысла, так это то, что он работает как автономное приложение для отправки sms, но если я интегрирую код в мой более крупный проект, он не отправит sms. Ничего не произошло. Что-то должно противоречить Smsmanager или что-то в этом роде. Может ли кто-нибудь дать мне совет, почему он больше не работает?Текст sms не отправляется

Android Manifest

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

SendSmsActivity.java

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(); 
       } 

      } 
     }); 
    } 
} 

activity_sms.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textViewPhoneNo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Enter Phone Number : " 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <EditText 
     android:id="@+id/editTextPhoneNo" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:phoneNumber="true" > 
    </EditText> 

    <TextView 
     android:id="@+id/textViewSMS" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Enter SMS Message : " 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <EditText 
     android:id="@+id/editTextSMS" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textMultiLine" 
     android:lines="5" 
     android:gravity="top" /> 

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

</LinearLayout> 

enter image description here

+0

Вы проверили логарифм (пожалуйста, не фильтруйте логарифм, иначе вы wmight пропустите что-то важное)? Откуда вы знаете, что это не работает? –

ответ

1

Попробуйте изменить свой код в списке клик ener к этому

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

Надеюсь, это поможет вам сделать работу.

+0

Это вызывает ошибку. См. Изображение, добавленное выше. – user6680

+0

Изменить 'this' на' SendSmsActivity.this' – cylon

+0

Я внес изменения и скомпилировал его, но когда я запустил его на своем телефоне, кнопка отправки все еще ничего не делает. Я обновил SendSmsActivity.java выше с новыми изменениями – user6680

Смежные вопросы