Как зарегистрировать широковещательный приемник внутри класса BroadcastReceiver AppWidgetProvider?

У меня есть это приложение AppWidget, в котором, когда пользователь щелкает его, он отправит SMS. Отправка СМС нажатием виджета работает нормально. Теперь я хочу знать, была ли отправка SMS успешной или неудачной. Как я это сделаю?

Вот код в отправке смс

String sms2 = id + " " + name + " " + cn.getName() + " " + message
                                    + " " + lati + " " + longi 
                                    + " https://maps.google.com/?q=" + lati + "," + longi + " -alertoapp";
                            String cp = cn.getPhoneNumber();
                            PendingIntent piSent=PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
                            PendingIntent piDelivered=PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
                            SmsManager sms = SmsManager.getDefault();
                            sms.sendTextMessage(cp, null, sms2, piSent, piDelivered);

Манифест

<receiver android:name=".Widget" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <!-- Broadcast Receiver that will also process our self created action -->
            <action android:name="de.thesmile.android.widget.buttons.ButtonWidget.ACTION_WIDGET_RECEIVER"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" />
    </receiver>

Я хочу использовать этот метод ниже, чтобы он работал с тостами, если SMS отправлено или нет, но проблема заключается в том, что метод registerReceiver недоступен в классе BroadCastReceiver.

 switch (getResultCode()) {
            case Activity.RESULT_OK:
                clear1();
                clear2();
                clear3();
                clear4();
                Toast.makeText(getBaseContext(), "SMS has been sent", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "Generic Failure", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "No Service", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "Radio Off", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getApplicationContext(), "Coordinates is null, Try again", Toast.LENGTH_LONG).show();
                break;
            }

        }
    };
    smsDeliveredReceiver=new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            switch(getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS Delivered", Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    };
    registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
    registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));

Любые предложения, ребята?


person Trij Estrelles    schedule 30.01.2014    source источник


Ответы (1)


Для тех, кто столкнулся с той же проблемой, что и я, я исправил ее с помощью это

Итак, вот оно

smsSentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context ctx, Intent arg1) {
        String msg = "";
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                msg = "SMS has been sent";
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                msg = "Generic Failure";
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                msg = "No Service";
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                msg = "Null PDU";
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                msg = "Radio Off";
                break;
            default:
                msg = "Coordinates is null, Try again";
                break;
        }
        Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }
};

smsDeliveredReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context ctx, Intent arg1) {
        String msg = "";
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                msg = "SMS Delivered";
                break;
            case Activity.RESULT_CANCELED:
                msg = "SMS not delivered";
                break;
        }
        Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }
};

ctx.getApplicationContext()
    .registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
ctx.getApplicationContext()
    .registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));

Вывод: Итак, прописать ресивер внутри класса BroadcastReceiver.

Вместо registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));

Используйте context.getApplicationContext().registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));

person Trij Estrelles    schedule 31.01.2014