如何使用接口将数据从Receiver传递到Activity?

时间:2016-03-17 17:33:57

标签: java android

我试图将数据从brodcast接收器传递到活动,以便使用界面在视图上显示它,但是,它无法正常工作。
这是我的文件,<登记/>
MainActivity.java

public class MainActivity extends AppCompatActivity implements OtpPassing {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onReceive(String msg) {
        Toast.makeText(this, "o" + msg, Toast.LENGTH_SHORT).show();
        textView = (TextView) findViewById(R.id.textView);
        textView.setText("OTP: " + msg);
    }
}


OtpPassing.java

public interface OtpPassing {
    public void onReceive(String msg);
}


Receiver.java

public class Receiver extends BroadcastReceiver {
    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {

        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);

                    String otp = getOtp(message);
                    Toast.makeText(context, otp, Toast.LENGTH_LONG).show();  //This is working fine
                    OtpPassing otpPassing = new MainActivity();
                    otpPassing.onReceive(otp); //This is not working

                } // end for loop
            } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);

        }
    }

    private String getOtp(String message) {
        String otp = null;
        int index = message.indexOf("otp:");
        if(index != -1){
            int start = index + 5;
            otp = message.substring(start, start+4);
        }
        return otp;
    }
}


接收器正在调用(它在Toast上显示消息)但是为什么通过MainActivity在屏幕上显示消息不起作用?

2 个答案:

答案 0 :(得分:0)

您可以将广播接收器创建为活动的数据成员。您在onCreate中注册它并在onDestroy(或onStart和onStop)中取消注册。

这样接收者可以参考父活动,你可以做任何事情。

public class MainActivity extends AppCompatActivity implements OtpPassing {

private TextView textView;
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
       //Access the parent activity as follows
       MainActivity.this.textView.setText("...");
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //instantiate and register the receiver
    setContentView(R.layout.activity_main);

}

@Override
protected void onDestroy() {
  // Unregister the receiver here
  super.onDestroy();
}

答案 1 :(得分:0)

这里更好的方式(我想)将使用Android Intents

在您的接收器中,类似这样的

Intent intent = new Intent(context,MainActivity.class);
intent.putExtra(.....);
startactivity(intent);

现在在您的MainActivity中,覆盖onNewIntent()。在这里检查你从接收器传递的'额外'。

请注意,如果您的“较旧”MainActivity在通过接收器传递此意图时未被杀死,则可能会导致手机中运行多个MainActivities。要解决此问题,请在清单文件中使用“singleTop”或类似的标志