2017-07-28 142 views
0

我想通過以下this教程構建一個短信應用程序,除了當我點擊send button時,其他用戶沒有收到message號碼, EditText。 在應用程序中,我有兩個EditText和一個button。其中一個EditText用於message,另一個用於指定接收器的phone number使用android應用程序發送短信

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ; 
    Button send; 
    EditText message; 
    EditText phoneno; 
    String number; 
    String txt; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     send=(Button)findViewById(R.id.sendButton); 
     message=(EditText)findViewById(R.id.textMessage); 
     phoneno=(EditText)findViewById(R.id.phone); 

     send.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       sendSMSMessage(); 
      } 
     }); 
    } 

    public void sendSMSMessage(){ 
     number=phoneno.getText().toString(); 
     txt=message.getText().toString(); 

     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.SEND_SMS) 
       != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
        Manifest.permission.SEND_SMS)) { 
      } else { 
       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.SEND_SMS}, 
         MY_PERMISSIONS_REQUEST_SEND_SMS); 
      } 
     } 

    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     //super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     switch (requestCode) { 
      case MY_PERMISSIONS_REQUEST_SEND_SMS: { 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        SmsManager smsManager = SmsManager.getDefault(); 
        smsManager.sendTextMessage(number, null, txt, null, null); 
        Toast.makeText(getApplicationContext(), "SMS sent.", 
          Toast.LENGTH_LONG).show(); 
       } else { 
        Toast.makeText(getApplicationContext(), 
          "SMS faild, please try again.", Toast.LENGTH_LONG).show(); 
        return; 
       } 
      } 
     } 

    } 
} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.shaloin.sample852.MainActivity"> 

<EditText 
    android:id="@+id/textMessage" 
    android:textSize="20sp" 
    android:layout_marginTop="20dp" 
    android:hint="Text Message" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
<EditText 
    android:id="@+id/phone" 
    android:layout_below="@+id/textMessage" 
    android:textSize="20sp" 

    android:layout_marginTop="20dp" 
    android:hint="Phone Number" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
<Button 
    android:id="@+id/sendButton" 
    android:layout_below="@+id/phone" 
    android:layout_centerHorizontal="true" 
    android:text="Send" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

的AndroidManifest.xml

:下面的代碼中給出10
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.shaloin.sample852"> 

<uses-permission android:name="android.permission.SEND_SMS"/> 
<uses-permission android:name="android.permission.RECEIVE_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=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

我曾嘗試使用內置的SMS應用程序發送的消息和它的作品,但我只是doesn't.Can任何人提出的申請幫助嗎?謝謝:)

回答

0

其實你沒有寫代碼在sendSMSMessage函數中發送消息,可能是第一次的消息可能發送消息。

public void sendSMSMessage() { 
    number = phoneno.getText().toString(); 
    txt = message.getText().toString(); 

    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.SEND_SMS) 
      != PackageManager.PERMISSION_GRANTED) { 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.SEND_SMS)) { 
     } else { 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.SEND_SMS}, 
        MY_PERMISSIONS_REQUEST_SEND_SMS); 
     } 
     return; 
    } 

    sendMessage(); 

} 

public void sendMessage() { 
    SmsManager smsManager = SmsManager.getDefault(); 
    smsManager.sendTextMessage(number, null, txt, null, null); 
    Toast.makeText(getApplicationContext(), "SMS sent.", 
      Toast.LENGTH_LONG).show(); 
} 
+0

所以,我必須寫在這兩個'onRequestPermissionsResult'和'sendSMSMessage'方法,或僅在'sendSMSMessage'方法發送消息的代碼? –

+0

您應該在這兩種方法中編寫代碼,您可以爲此常見代碼段創建一個單獨的方法,並從這兩個地方調用。 –

+0

非常感謝你!有效 –