0

I wan't to build SMS triggered app in my android. I know there's a lot app in the market to do this but I Just want to try it on my own.

My SMS receiver project is working as well as my Caller project. I want to combine them as 1 application but I'm out of idea. can someone give me a hand?

here is the code snippet I got from somewhere and tested working..

Caller.java

package pi.redphone;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;



public class Caller extends Activity 
{
    private Context context;

    /** Called when the activity is first created. */
    @Override
       public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String num = "2217238";
        call(num);
    } 

 public void call(String number) 
{
    try 
    {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:"+number));
        startActivity(callIntent);
    } 
    catch (ActivityNotFoundException activityException) 
    {
        Log.e("dialing-example", "Call failed", activityException);
    }
}

}

the phone number in the code above is hard coded and it will call to this number after installation 2217238

SmsReceiver.java

package pi.redphone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver
{


    public void onReceive(Context context, Intent intent) 
    {
        Bundle bundle = intent.getExtras();
        //SmsMessage msg = null;
        String sender = null;
        String msgBody = null;

        if(bundle != null)
        {
            Object[] sms = (Object[]) bundle.get("pdus");

            for(int i=0; i<sms.length; i++)
            {
                SmsMessage msg = SmsMessage.createFromPdu((byte[]) sms[i]);
                sender = msg.getOriginatingAddress(); //store sender's mobile #
                msgBody = msg.getMessageBody(); //msg content

            }
            Toast.makeText(context, sender, Toast.LENGTH_LONG ).show();


        }

    }

}

after the installation, the app will give this error

The application Callforwarding has stoped working unexpectedly. Pls try again..

Actually what I want to do is when someone send sms to my android my SmsReceiver will extract the sender mobile number and it will call back.. the sender's number is stored in theh sender variable..

I want to do something like this

call(sender);

But It's not easy as that.. I can combine the source code with no errors but I cannot get it working after the installation...

This would be my first Android application if I can get this to work.. ;)


This is my manifest file:

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

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_SMS">

    </uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".callForward"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And this is my LogCAt:

01-15 03:11:50.265: I/jdwp(289): Ignoring second debugger -- accepting and dropping
01-15 03:12:30.756: W/KeyCharacterMap(289): No keyboard for id 0
01-15 03:12:30.756: W/KeyCharacterMap(289): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-15 03:13:05.095: D/dalvikvm(289): GC_EXPLICIT freed 1347 objects / 90832 bytes in 74ms
01-15 03:18:25.995: D/AndroidRuntime(357): Shutting down VM
01-15 03:18:26.045: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-15 03:18:26.115: E/AndroidRuntime(357): FATAL EXCEPTION: main
01-15 03:18:26.115: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{pi.redphone/pi.redphone.callForward}: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.os.Looper.loop(Looper.java:123)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-15 03:18:26.115: E/AndroidRuntime(357):  at java.lang.reflect.Method.invokeNative(Native Method)
01-15 03:18:26.115: E/AndroidRuntime(357):  at java.lang.reflect.Method.invoke(Method.java:521)
01-15 03:18:26.115: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 03:18:26.115: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 03:18:26.115: E/AndroidRuntime(357):  at dalvik.system.NativeStart.main(Native Method)
01-15 03:18:26.115: E/AndroidRuntime(357): Caused by: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
01-15 03:18:26.115: E/AndroidRuntime(357):  ... 11 more
01-15 03:18:35.085: I/Process(357): Sending signal. PID: 357 SIG: 9
01-15 03:31:01.475: D/AndroidRuntime(449): Shutting down VM
01-15 03:31:01.505: W/dalvikvm(449): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-15 03:31:01.555: E/AndroidRuntime(449): FATAL EXCEPTION: main
01-15 03:31:01.555: E/AndroidRuntime(449): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{pi.redphone/pi.redphone.callForward}: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.os.Looper.loop(Looper.java:123)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-15 03:31:01.555: E/AndroidRuntime(449):  at java.lang.reflect.Method.invokeNative(Native Method)
01-15 03:31:01.555: E/AndroidRuntime(449):  at java.lang.reflect.Method.invoke(Method.java:521)
01-15 03:31:01.555: E/AndroidRuntime(449):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 03:31:01.555: E/AndroidRuntime(449):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 03:31:01.555: E/AndroidRuntime(449):  at dalvik.system.NativeStart.main(Native Method)
01-15 03:31:01.555: E/AndroidRuntime(449): Caused by: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
01-15 03:31:01.555: E/AndroidRuntime(449):  ... 11 more
01-15 03:31:15.255: I/Process(449): Sending signal. PID: 449 SIG: 9

The application still don't run. here is my whole source code.

package pi.redphone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class callForward extends BroadcastReceiver
 {
    /** Called when the activity is first created. */
    public void onReceive(Context context, Intent intent) 
    {
        Bundle bundle = intent.getExtras();
        //SmsMessage msg = null;
        String sender = null;
        String msgBody = null;

        if(bundle != null)
        {
            Object[] sms = (Object[]) bundle.get("pdus");

            for(int i=0; i<sms.length; i++)
            {
                SmsMessage msg = SmsMessage.createFromPdu((byte[]) sms[i]);
                sender = msg.getOriginatingAddress(); //store sender's mobile #
                msgBody = msg.getMessageBody(); //msg content

            }
            Toast.makeText(context, sender, Toast.LENGTH_LONG ).show();


        }

    }

}

I have a feeling that the code is correct but I cannot figure out how to solve the issue..


I don't know how to follow your suggestion because I'm always getting error. but instead help me to check if I'm doing right..

this is my latest code. call is functioning but my sms receiver don't.

I dont know how to call my

public class SmsReceiver extends BroadcastReceiver 

in the activity so that it listen to incoming sms.

Call4wardActivity.Java

package pi.redphone;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class Call4wardActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            call();

        }



    public void call() 
    {
        try {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:123456789"));
            startActivity(callIntent);
        } catch (ActivityNotFoundException activityException) {
             Log.e("helloandroid dialing example", "Call failed", null);
        }
    }

    public class SmsReceiver extends BroadcastReceiver 
    {


        @Override
        public void onReceive(Context context, Intent intent) 
        {
            // TODO Auto-generated method stub
            Bundle bundle = intent.getExtras();        
            SmsMessage[] msgs = null;
           // String body = null;
            String sender = null;


            if (bundle != null)
            {
                //---retrieve the SMS message received---
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];   

                for (int i=0; i<msgs.length; i++)
                {
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);  
                    sender = msgs[i].getOriginatingAddress();

                }
                //---display the new SMS message---
                Toast.makeText(context, sender, Toast.LENGTH_LONG).show();

            }                       
        }

        }


    }

manifest file

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

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Call4wardActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

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

</manifest>

2 Answers 2

2

You'll have to post your logcat error for help with the crashing. But making a phone call isn't too hard. My initial guess is that you are missing permissions in your android manifest. Does it have all the permissions from the SMS and Dialer apps?

Make sure that sender is storing only a phone number. I believe it is though.

You really don't need to include the first application. The only code you need is

try  
{  
    Intent callIntent = new Intent(Intent.ACTION_CALL);  
    callIntent.setData(Uri.parse("tel:"+number));  
    startActivity(callIntent);  
}   
catch (ActivityNotFoundException activityException)   
{  
    Log.e("dialing-example", "Call failed", activityException);  
}  

Replace number with sender and you should be all set.

If you want to keep both Activities you can do the following. Intent callIntent = new Intent(this, Caller.class );
callIntent.putExtra("phoneNumber", sender);
startActivity(callIntent);

In the call activity, you would

Bundle extras = getIntent().getExtras();  
string sender = extras.getInt("phoneNumber");  

Then you can prompt the user and ask if u want the app to call the number. Or you can just call it right away. If you're just calling the # right away without confirmation, use the 1st solution mentioned above in my post.

5
  • I got the sms receiver working but how should i call the startActivity(callIntent); ?
    – mboy
    Jan 14, 2012 at 20:44
  • I've added the edits. I don't know if you want to call automatically or launch your app that has a call button. But I've provided code to launch the caller activity and send the phone number Jan 14, 2012 at 21:15
  • I've added edits above too my current work... Question about this. callIntent = new Intent(this, Caller.class ); callIntent.putExtra("phoneNumber", sender); You told me not to use the first file but why using Caller class which is in the other file? sorry I can't get it..
    – mboy
    Jan 15, 2012 at 15:56
  • You're solution was exactly what I was trying to help you get towards. I had ambiguous code because it seemed like you were adamant about keeping the startActivity(callIntent). You NEVER needed that calling activity is what I was explaining to you. Jan 30, 2012 at 2:07
  • Thanks. now I hava another problem about some special character for the call.. Pls check my other post here stackoverflow.com/questions/9048833/…
    – mboy
    Jan 30, 2012 at 6:15
-1

I finally solved this.. :)

here is the basic code.. it will call the SMS sender..

SmsReceiver.java

package romel.pi.redphone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsReceiver extends BroadcastReceiver 
{

    private Context context;

    private static final String frwdcode = "**21*";
    String phoneNumber = "";
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        System.out.println("Starting Receiver");

        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                //str += "SMS from " + msgs[i].getOriginatingAddress();  
                phoneNumber = msgs[i].getOriginatingAddress();
                str += "\n\nMessage:\n\n";
                str += msgs[i].getMessageBody().toString();
                str += "\n";        
            }
            try 
            {   

                Log.v("SmsReceiver", "entering TRY");
                this.context = context;
                MessageBodyParserTrigger();







            } 
            catch (Exception e) 
            {   
                Log.v("SendMail", e.getMessage(), e);   
            } 
        } 


    }

    void MessageBodyParserTrigger()
    {

        Object localObject1 = new Intent("android.intent.action.CALL");
        Object localObject2 = Uri.encode("#");
        ((Intent)localObject1).setData(Uri.parse("tel:"+ "**21*" + phoneNumber + localObject2));
        ((Intent)localObject1).addFlags(268435456);
        this.context.startActivity((Intent)localObject1);
        this.context.stopService(null);

    }



}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="romel.pi.redphone"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >




        <receiver android:name=".SmsReceiver"> 
            <intent-filter> 
                <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
            </intent-filter> 
        </receiver>





    </application>




</manifest>

hope it can help to others..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.