Monday, November 28, 2016

Android : Send SMS


Add below line in Manifest file
<uses-permission android:name="android.permission.SEND_SMS" />
 
 Eg Manifest File:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.dejayapr.test1">

    <!-- To auto-complete the email text field in the login form with the user's emails -->    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.SEND_SMS" />

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

    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        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>
        <activity android:name=".new_Activity" />
        <activity android:name=".Allusers"></activity>
    </application>

</manifest>
 
 
 
Code below :
 
import android.Manifest;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
 
 
public void sendSMS_simple(String email,String password){
    try {
        PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, Object.class), 0);
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(this.phoneNo, null, email +"\n"+ password, null, null);
        Log.i("sms","SMS Sent - sendSMS_simple()"); //title - has to be in smaller case in Log.d,v,i,
    }catch(Exception e){
        Log.d("Exception Happened",e.getMessage());
    }
}

No comments:

Post a Comment