开发者

Is it possible to receive SMS message on appWidget?

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, n开发者_如何学JAVAew IntentFilter("android.provider.Telephony.SMS_RECEIVED"));

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

Any help would be appreciated!!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜