I am trying to send a simple sms. The thing is it is working on the samples that i downloaded from tutorials. But when I am trying to replicate the same code, I am getting error. Below is the code when I tried to debug.
D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
W/System.err: java.lang.SecurityException: Sending SMS message: uid 10333 does not have android.permission.SEND_SMS.
W/System.err: at android.os.Parcel.readException(Parcel.java:1620)
W/System.err: at android.os.Parcel.readException(Parcel.java:1573)
W/System.err: at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:1577)
W/System.err: at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:380)
W/System.err: at android.telephony.SmsManager.sendTextMessage(SmsManager.java:333)
W/System.err: at com.creations.oreo.valletcall.MainActivity$1.onClick(MainActivity.java:40)
W/System.err: at android.view.View.performClick(View.java:5697)
W/System.err: at android.widget.TextView.performClick(TextView.java:10814)
W/System.err: at android.view.View$PerformClick.run(View.java:22526)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:158)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7229)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Disconnected from the target VM, address: 'localhost:8616', transport: 'socket'
I am trying to debug by connecting an actual phone and the permission error probably the obvious one from the above is actually already defined in the Android Manifest file.
My MainActivity as below
public class MainActivity extends AppCompatActivity {
Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
/* Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);*/
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("91195525", null, "Test", null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later SMS!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
}
My Android Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.creations.oreo.valletcall">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The tutorial for example I am following is this
http://www.mkyong.com/android/how-to-send-sms-message-in-android/
From the official doc
And you can read the result with
This way you are asking for permissions runtime, now you gotta implmeent those methods to make them work in your app.
Hope this helps.
Note: another way for sending sms is through intents