将第二个活动类的输出转换为第一个活动调用的第一个活动

时间:2015-12-15 21:14:22

标签: java android android-intent

我的代码有两项活动。

public class OnCallingActivity extends Activity implement TextToSpeech.OnInitListener{}

public class TextReceiverActivity extends BroadcastReceiver{}

我想要第一项活动来扩展第二项活动

OnCallingActivity extends TextReceiverActivity {}

所以我这样做了,

public class OnCallingActivity extends Activity implements TextToSpeech.OnInitListener {

  private TextReceiverActivity TextReceiverActivityClass;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.on_calling_layout);

    TextReceiverActivityClass= new TextReceiverActivity();
    speaker= new TextToSpeech(this, this);
    callerIdText=(TextView)findViewById(R.id.callerIdtextview);
    callerText=(TextView)findViewById(R.id.callermsgtextview);
    translatedText=(TextView)findViewById(R.id.translatedTextview);
    speakButton=(ImageButton)findViewById(R.id.btnSpeak);
    sendButton=(Button)findViewById(R.id.buttonsend);
    endCallButton=(ImageButton)findViewById(R.id.buttonendCall);        
}

我的第二项活动如下,

public class TextReceiverActivity extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
     number ="";
     message="";

     final Bundle bundle = intent.getExtras(); 

     try {
        if(bundle != null) {
            final Object[] pduObjects = (Object[]) bundle.get("pdus");

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

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pduObjects[i]);

                number = currentMessage.getDisplayOriginatingAddress();
                message = currentMessage.getDisplayMessageBody();

                Toast.makeText(context, "SENDER NUMBER: " + number + "; MESSAGE: " + message, Toast.LENGTH_LONG).show();
            }               
        }
     } catch (Exception e) {
        e.printStackTrace();
     }
  }

在第一个活动中,我调用了第二个活动的方法,如此

public void onReceive(Context context, Intent intent) { 

    TextReceiverActivityClass.onReceive(context, intent);       

}

onReceive给出了数字和消息的值。所以如何让它们进入我的第一个活动,以便在TextViews中显示它们。

onReceive是一个内置方法,因此我无法更改返回类型。

1 个答案:

答案 0 :(得分:0)

  1. 如果Public Sub MakeCompareSheet() Dim i As Long Dim rCell As Range Dim shBuild As Worksheet Dim shComp As Worksheet Set shBuild = Sheets("Builds") Set shComp = Sheets("Build Compare") For i = 2 To 8 Step 2 'cols B through H 'loop through row 1 of the builds sheet For Each rCell In shBuild.Range("A1:Z1").Cells 'if row 1 of builds = row 1, column i of comps 'and if the cell below equals the cell below If rCell.Value = shComp.Cells(1, i).Value And rCell.Offset(1, 0).Value = shComp.Cells(2, i).Value Then 'copy row three down 166 rows to the comps wheet rCell.Offset(2, 0).Resize(166, 1).Copy shComp.Cells(3, i) 'Since we already found it, we don't need to look anymore Exit For End If Next rCell Next i End Sub ,则TextReceiverActivity不是一个Activity,我不会将其称为Activity。

  2. BroadcastReceiver的TextReceiverActivity extends BroadcastReceiver方法是从Android系统调用的,而不是你。

  3. 要将消息从活动发送到BroadcastReceiver,您将发送广播。见the docs

  4. 如果您想从BroadcastReceiver向您的活动发送消息,那么couple questions about就是{{3}}。