Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am new to android development.I am developing small android application. In my application I want to retrieve newly coming sms and display this message to user. My code looks like

// HellowordActivity.java
package com.example.helloword;

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

public class HellowordActivity extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
    Bundle myBundle = intent.getExtras();
    SmsMessage [] messages = null;
    String strMessage = "";

    if (myBundle != null)
    {
        Object [] pdus = (Object[]) myBundle.get("pdus");
        messages = new SmsMessage[pdus.length];

        for (int i = 0; i < messages.length; i++)
        {
            messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            strMessage += "SMS From: " + messages[i].getOriginatingAddress();
            strMessage += " : ";
            strMessage += messages[i].getMessageBody().toString();
            strMessage += "
";
        }
         // Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();

        Intent _intent = new Intent(context, PopupActivity.class);
        _intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        _intent.putExtra("strMessage", strMessage);
        startActivity(_intent);
       }
    }

  }  

I added receiver and permission in Android Manifest.xml

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

      <receiver android:name=".HellowordActivity" >
                <intent-filter > 
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
                </intent-filter> 
        </receiver> 
<activity android:name=".PopupActivity" android:launchMode="singleTop" />

I am not doing any thing in layout part.What I want as an output when new message will come; message text display to user with simple popup. Need Help.. Thank you...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
130 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this it works for me you will get a toast shown to you with the content of the message received:

package com.example.a;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

    public class SMSBroadcastReceiver extends BroadcastReceiver {

            private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
            private static final String TAG = "SMSBroadcastReceiver";

            @Override
            public void onReceive(Context context, Intent intent) {
                 Log.i(TAG, "Intent recieved: " + intent.getAction());

                    if (intent.getAction().equals(SMS_RECEIVED)) {
                        Bundle bundle = intent.getExtras();
                        if (bundle != null) {
                            Object[] pdus = (Object[])bundle.get("pdus");
                            final SmsMessage[] messages = new SmsMessage[pdus.length];
                            for (int i = 0; i < pdus.length; i++) {
                                messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                            }
                            if (messages.length > -1) {
                                Toast.makeText(context, "Message recieved: " + messages[0].getMessageBody(), 7000).show();
                            }
                        }
                    }
               }
        }

The AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a"
    android:versionCode="1"
    android:versionName="1.0" >

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


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name=".SMSBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" >
                </action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

Use DDMS to send sms to your emulator via Telnet


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...