如何在其他非活动类Android中的Activity中插入TextView?

时间:2017-03-18 04:54:28

标签: java android textview

我需要从TextView(非活动类)设置Main3Activity {Activity}的IncomingSms个对象。

public class IncomingSms extends BroadcastReceiver {

    final SmsManager sms = SmsManager.getDefault();

    @Override
    public void onReceive(Context context, Intent 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 senderNum = currentMessage.getDisplayOriginatingAddress();
                    String message = currentMessage.getDisplayMessageBody();
                    if (message.equalsIgnoreCase("FIRE DETECTED")) {
                        //Problem start here: I cant able to set TextView object of Main3Activity
                        Main3Activity main3Activity = new Main3Activity();
                        TextView tv = (TextView) main3Activity.findViewById(R.id.firealert);
                        tv.setText(message);
                        //Problem end here
                        Log.i("SmsReceived", "senderNum: " + senderNum + "; message: " + message);
                        int duration = Toast.LENGTH_LONG;
                        Toast toast = Toast.makeText(context, message, duration);
                        toast.show();
                    }
                }
            }
        } catch(Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" + e);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您无法像这样创建活动实例。

解决方案: 1.在您的活动中创建广播接收器并注册自定义意图。 2.从短信接收器发送自定义广播意图。所以基本上它会到达活动接收器,然后你可以简单地更新那里的文本。

如果您的活动未启动,只需创建一个活动意图并将短信文本作为意图附加内容的一部分传递。

答案 1 :(得分:1)

创建一个接口类,将其保存为java类,然后

public interface SmsListener {
            public void messageReceived(String messageText);
    }

final SmsManager sms = SmsManager.getDefault();
private static SmsListener mListener;
@Override
public void onReceive(Context context, Intent 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 senderNum = currentMessage.getDisplayOriginatingAddress();
                String message = currentMessage.getDisplayMessageBody();
                if (message.equalsIgnoreCase("FIRE DETECTED") ){
                    mListener.messageReceived(message); //add this
                    Log.i("SmsReceived", "senderNum: "+ senderNum + "; message: " + message);
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, message, duration);
                    toast.show();
                }
            }
        }
    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);
    }
}
public static void bindListener(SmsListener listener) {
        mListener = listener;
    }

在您的Main3Activity onCreate方法

TextView tv=(TextView) main3Activity.findViewById(R.id.firealert);


SmsReceiver.bindListener(new SmsListener() {
                @Override
                public void messageReceived(String messageText) {
                    Log.d("Text",messageText);
                     tv.setText(messageText);
                }
            });