3

I have built a test app that upon button tap sends an automated SMS with pre-defined message to a pre-defined phone number. I have used intents.

But when I tap 'button' it crashes. Any ideas? Am I missing something or implementing wrong?

CODE:

package com.ali.sms;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    Button button;

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

        button=(Button)findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            // TODO Auto-generated method stub
                Sms();
            }
        });
    }

    void Sms(){

        Intent intent = new Intent( Intent.ACTION_SENDTO, Uri.parse("sms:0123456789"));
        intent.putExtra("sms_body", "Hello!");
        intent.setType("vnd.android-dir/mms-sms");
        startActivity(intent);
    }
}

Manifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ali.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="22" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

In Eclipse/LogCat I'm getting this error:

android.content.ActivityNotFoundException: No Activity found to handle Intent {act=android.intent.action.SENDTO typ=vnd.android-dir/mms-sms (has extras) }

On phone error message is:

Unfortunately, SMS has stopped.

P.S. I have searched the web & stackoverflow for fixing No Activity found to handle Intent but to no avail.

bummi
  • 27,123
  • 14
  • 62
  • 101
Ali Khan
  • 633
  • 1
  • 7
  • 18

1 Answers1

1

Please check this related question. Send SMS via intent

The answer seems to be to simply remove the intent type.

 public void Sms() { 
     Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:0123456789")); 
     intent.putExtra("sms_body", "Hello!"); 
     startActivity(intent); 
  }

In order to have automatic sending then consider using the SmsManager.sendTextMessage method.

    SmsManager sm = SmsManager.getDefault();
    String destinationAddress = "012345679";
    String text = "Hello";
    sm.sendTextMessage(destinationAddress, null, text, null, null);
Community
  • 1
  • 1
Derek
  • 1,572
  • 1
  • 14
  • 25
  • Ok, thanks, it worked. The app is no longer crashing but still no automated sms sending! It just redirects me to default sms app with message & number pre-entered :( – Ali Khan Apr 22 '15 at 13:06