Sms 在android中收到消息时如何调用另一个类

Sms 在android中收到消息时如何调用另一个类,sms,Sms,这是我的短信接收器 package com.example.smsTest; 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 c

这是我的短信接收器

package com.example.smsTest;


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 += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";        
        }
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

        Intent mainActivityIntent = new Intent(context, SMSTest.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);

    }                       
}
}
package com.example.smsTest;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.sms消息;
导入android.widget.Toast;
公共类SmsReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图)
{
//---获取传入的SMS消息---
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];

对于
SmsReceiver
中的(int i=0;i),请执行以下操作:

Intent mainActivityIntent = new Intent(context, SMSTest.class);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainActivityIntent.putExtra("sms", str);
context.startActivity(mainActivityIntent);
然后,在活动的
onCreate()
方法中:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView SMSes = (TextView) findViewById(R.id.textView1);
    String sms = getIntent().getStringExtra("sms");
    SMSes.setText(sms);
}
您在清单中的
中拼错了“RECEIVED”,因此您的BroadcastReceiver没有启动。它应该是:

<action android:name="android.provider.Telephony.SMS_RECEIVED" />


Thanq,当收到消息时,仅在收件箱中显示也不起作用。不启动其他活动。是的,它没有启动指定的活动。只是短信的默认应用程序显示传入消息。如果您请求调用活动的目的是:private BroadcastReceiver intentReceiver=new BroadcastReceiver(){@Override public void onReceive(上下文,意图){/--在TextView中显示收到的短信---TextView SMSes=(TextView)findViewById(R.id.textView1);SMSes.setText(intent.getExtras().getString(“sms”);};我在《android初学者》一书中看到过一个教程,他们说要调用该活动。所以我只是复制了该代码。但上面提到的内容只会出现。不启动该活动当收到消息时,会发出通知,当消息被打开时,它会在android中已经存在的消息应用程序中打开。
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView SMSes = (TextView) findViewById(R.id.textView1);
    String sms = getIntent().getStringExtra("sms");
    SMSes.setText(sms);
}
<action android:name="android.provider.Telephony.SMS_RECEIVED" />