0

我的原始应用程序接收文本并使用字符拆分来拆分并显示在文本视图中。然后,我实现了从 BroadcastReceiver 调用活动的代码,以便在收到文本时将活动带到最前面,但应用程序的文本视图没有更新。

如果我从 SMSReceiver.java 中删除,它可以正常工作,但应用程序不会被带到前面。

//--Launch the MainActivity--//
        Intent mainActivityIntent = new Intent(context, MainActivity.class);
        mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mainActivityIntent);

SMSReceiver.java 代码:

package com.example.smssend;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {


//--Get the SMS Message passed in---//
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        if (bundle != null)
        {


//--Retrieve the SMS message received--//
            Object[] pdus = (Object[] ) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i=0; i<msgs.length; i++)
            {


msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                str += msgs[i].getMessageBody().toString();


}
            //--Display the new SMS Message--//
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            String sender = null;



//--Launch the MainActivity--//
            Intent mainActivityIntent = new Intent(context, MainActivity.class);


mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    context.startActivity(mainActivityIntent);


    //--Send a broadcast intent to update the SMS received in the activity--//


    Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("SMS_RECEIVED_ACTION");
            broadcastIntent.putExtra("sms", str);
            context.sendBroadcast(broadcastIntent);
        } 
    }
}
4

2 回答 2

1

可能当您发送广播意图时,活动尚未加载或绑定到任何接收器。

您可以避免在 mainActivityIntent 中传递消息的问题,而不是使用 broadcastIntent 来更新 Activity。

您可以在 Activity 中使用 getIntent() 恢复意图,然后更新 UI。

Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainActivityIntent.putExtra("sms", str);
context.startActivity(mainActivityIntent);

在您的活动中

    @Override
    protected void onResume() 
    {
            super.onResume();
            Intent myIntent = getIntent();
            String sms = myIntent.getStringExtra("sms");
            if (sms != null && !"".equals(sms))
            {
                            mTextView.setText(sms);
            }
    }

希望能帮助到你 :)

于 2013-11-05T15:26:55.027 回答
0

主要活动代码:

package com.example.smssend;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {


Button btnSendSMS;
IntentFilter intentFilter;
String textView1;
String textView2;
String textView3;

private BroadcastReceiver intentReceiver = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent){
        //--Display the SMS received in the TextView1--//

        TextView SMSes3 = (TextView) findViewById(R.id.textView1);  
        TextView SMSes2 = (TextView) findViewById(R.id.textView3);
        TextView SMSes1 = (TextView) findViewById(R.id.textView2);
        textView3 = intent.getExtras().getString("sms");
        char arr[] = textView3.toCharArray();
        String String1 = new String(arr);
        String[] parts = String1.split(" ");
        String part1 = parts[0];
        String part2 = parts[1];
        String part3 = parts[2];
        SMSes2.setText(part2);
        SMSes1.setText(part1);
        SMSes3.setText(part3);

    }
};

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //--Intent to filter for SMS messages received--//
    intentFilter = new IntentFilter();
    intentFilter.addAction("SMS_RECEIVED_ACTION");

    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
    btnSendSMS.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            sendSMS("92200026", "Hello");
        }


    });
}

@Override
protected void onResume(){
    //--Register the receiver--//
    registerReceiver(intentReceiver, intentFilter);
    super.onResume();
}

@Override
protected void onPause(){
    //--Unregister the receiver--//
    unregisterReceiver(intentReceiver);
    super.onPause();
}

//--Send an SMS message to another device---//
private void sendSMS(String phoneNumber, String message)
{
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

于 2013-11-11T01:36:57.540 回答