0

i am trying to develop a simple location tracking application and i have used broadcastReceivers in it. i am using simple SMS sending method to get the location of the device. i am trying to get feedback after sending and receiving SMS. but i am getting error while registering the receivers. error which is i am getting is The method registerReceiver(BroadcastReceiver, IntentFilter) is undefined for the type SMSReceiver.MyLocationListener. please help me with this.

Here is my code:

private class MyLocationListener implements LocationListener
    {

        @Override
        public void onLocationChanged(Location loc) 
        {

            if(loc!=null)
            {
                //send a sms containing the current location
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(senderTel, null, "http://maps.google.com/maps?q" + loc.getLatitude() + "," + loc.getLongitude(), null, null);
                BroadcastReceiver smsSentReceiver = new BroadcastReceiver()
                {

                    @Override
                    public void onReceive(Context arg0, Intent arg1) 
                    {
                        switch(getResultCode())
                        {
                            case Activity.RESULT_OK:
                                Toast.makeText(arg0, "SMS sent", Toast.LENGTH_LONG).show();
                                break;
                            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                                Toast.makeText(arg0, "generic failure", Toast.LENGTH_LONG).show();
                                break;
                            case SmsManager.RESULT_ERROR_NO_SERVICE:
                                Toast.makeText(arg0, "No service", Toast.LENGTH_LONG).show();
                                break;
                            case SmsManager.RESULT_ERROR_NULL_PDU:
                                Toast.makeText(arg0, "Null PDU", Toast.LENGTH_LONG);
                                break;
                            case SmsManager.RESULT_ERROR_RADIO_OFF:
                                Toast.makeText(arg0, "Radio Off", Toast.LENGTH_LONG).show();
                                break;
                        }

                    }

                };
                // stop listening for location change
                lm.removeUpdates(locationListener);
                smsDeliveredReceiver= new BroadcastReceiver()
                {

                    @Override
                    public void onReceive(Context arg0, Intent arg1) 
                    {
                        switch(getResultCode())
                        {
                        case Activity.RESULT_OK:
                            Toast.makeText(arg0, "SMS delivered", Toast.LENGTH_LONG).show();
                            break;
                        case Activity.RESULT_CANCELED:
                            Toast.makeText(arg0, "SMS not delivered", Toast.LENGTH_LONG).show();
                            break;
                        }

                    }

                };
                registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));
4

1 回答 1

7

您需要一个上下文来调用 registerReceiver(); ,例如 Activity 或服务。

这将是这样的:

theContext.registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));

于 2013-08-21T18:14:10.647 回答