views:

123

answers:

1

Is it possible to receive SMS message on appWidget?

I saw android sample source(API Demos).

In API Demos, ExampleAppWidgetProvider class extends AppWidgetProvider, not Activity.

So, I guess it is impossible to regist SMS Receiver like this,

rcvIncoming = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("telephony", "SMS received");
        Bundle data = intent.getExtras();
        if (data != null) {
            // SMS uses a data format known as a PDU
            Object pdus[] = (Object[]) data.get("pdus");
            String message = "New message:\n";
            String sender = null;
            for (Object pdu : pdus) {
                SmsMessage part = SmsMessage.createFromPdu((byte[])pdu);
                message += part.getDisplayMessageBody();
                if (sender == null) {
                    sender = part.getDisplayOriginatingAddress();
                }
            }

            Log.i(sender, message);

        }
    }
};

registerReceiver(rcvIncoming, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));

My goal is to receive SMS message on my custom appWidget.

Any help would be appreciated!!

+1  A: 

AppWidgetProvider is a derived class of BroadcastReceiver. Therefore, you can override onReceive() to handle SMS message. Of course, you have to setup intent filter for SMS_RECEIVED in the manifest correctly first.

Patrick Tsai
Thank you for your advice. I completely forgot that I could use IntentFilter in the manifest!
cappuccino