Sushan Ghimire
Sushan Ghimire

Reputation: 7567

can text messages be opened directly on third party app (android)?

when a text message is sent form, lets say an application 'myApp' it'll open in default text message app of the receiver. but i want to control how it looks to receiver(like changing colour). Is there anyway to send text and read that text in native app, 'myApp'? Or identify it was sent from 'myApp' and import message to 'myApp'.

Upvotes: 0

Views: 313

Answers (1)

confucius
confucius

Reputation: 13327

sure you can to receive messages make a broadcast receiver for ingoing messages an each time a message arrive start your activity that displays the message ...

public class SMSApp extends IntentReceiver {
    private static final String LOG_TAG = "SMSApp";

    /* package */ static final String ACTION =
            "android.provider.Telephony.SMS_RECEIVED";

    public void onReceiveIntent(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {
            StringBuilder buf = new StringBuilder();
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
                for (int i = 0; i < messages.length; i++) {
                    SmsMessage message = messages[i];
                    buf.append("Received SMS from  ");
                    buf.append(message.getDisplayOriginatingAddress());
                    buf.append(" - ");
                    buf.append(message.getDisplayMessageBody());
                }
            }
           //start you messages activity 

        Intent i = new Intent();
        i.setClassName("com.test", "com.test.myMessagesAcivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        //prepare message text to be sent to the activity via bundle 
        Bundle bundle = new Bundle();
        bundle.putString("message", but.toString());
        i.putExtras(bundle);
        context.startActivity(i);


        }
    }


}

and in your manifest file add these permissions

<uses-permission android:id="android.permission.RECEIVE_SMS" />

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

and this receiver

<receiver class="SMSApp">

            <intent-filter>

                <action android:value="android.provider.Telephony.SMS_RECEIVED" />

            </intent-filter>

        </receiver>

and to send SMS from your app

use this method

public void eb3atSMS(String phoneNumber, String message)
    {        

        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, **DummyClasshere.class**), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);        
    }    

Upvotes: 1

Related Questions