在Android上发送和接收邮件错误

时间:2012-12-30 16:52:25

标签: android

我正在尝试编写程序,并且它的某些部分发送和接收消息。问题是,当我调用ReceiveSMS类时,它会给出这个错误:

  

android.content.ActivityNotFoundException:无法找到显式活动类{ir.hadisoft.paya / ir.hadisoft.paya.ReceiveSMS};你有没有在AndroidManifest.xml中声明这个活动?

这是我的receivems课程:

package ir.hadisoft.paya;

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

public class ReceiveSMS extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
            android.util.Log.d("test","test");

            //--- 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";
                }
                //--- Display the new SMS message ---
                Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            }
        }
    }
}

这是我的manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ir.hadisoft.paya"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS" ></uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".Paya"
            android:label="@string/title_activity_paya" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".Activation"></activity>

        <receiver android:name=".ReceiveSMS" android:enabled="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" >
                </action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

这是sendms类:

package ir.hadisoft.paya;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.SmsManager;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class Activation extends Activity implements OnClickListener {
    public String fonts = "BZar.ttf";
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activation);
        setFace();
    }

    protected void setFace()
    {
        View view_confirm_activation = findViewById(R.id.btn_confirm_activation);
        view_confirm_activation.setOnClickListener(this);

        View view_return_main = findViewById(R.id.btn_return_main);
        view_return_main.setOnClickListener(this);

        Typeface face = Typeface.createFromAsset(getAssets(), "font/"+fonts+"");

        //-------------------------------------------------

        TextView lbl_activation = (TextView)findViewById(R.id.lbl_activation);
        lbl_activation.setTypeface(face);
        String str_lbl = (String) lbl_activation.getText().toString();
        lbl_activation.setText(PersianReshape.reshape(str_lbl));

        //-------------------------------------------------

        Button btn_confirm_activation = (Button)findViewById(R.id.btn_confirm_activation);
        btn_confirm_activation.setTypeface(face);
        String str_confirm_activation = (String) btn_confirm_activation.getText().toString();
        btn_confirm_activation.setText(PersianReshape.reshape(str_confirm_activation));

        //-------------------------------------------------

        Button btn_return_main = (Button)findViewById(R.id.btn_return_main);
        btn_return_main.setTypeface(face);
        String str_return_main = (String) btn_return_main.getText().toString();
        btn_return_main.setText(PersianReshape.reshape(str_return_main));

        //-------------------------------------------------

        TextView enter_code_first = (TextView)findViewById(R.id.lbl_enter_code_first);
        enter_code_first.setTypeface(face);
        String str_enter_code = (String) enter_code_first.getText().toString();
        enter_code_first.setText(PersianReshape.reshape(str_enter_code));

    }

    protected void send_sms()
    {
        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.getDeviceId();
        String phoneNumber = "09127517737";
        EditText edit_activation = (EditText)findViewById(R.id.edit_activaiton);
        String serialnumber = edit_activation.getText().toString();

        if(serialnumber.equalsIgnoreCase(""))
        {
            TextView lbl_enter_code_first=(TextView)findViewById(R.id.lbl_enter_code_first);
            lbl_enter_code_first.setVisibility(View.VISIBLE);
        }
        else
        {
            String phoneIMEI = telephonyManager.getDeviceId().toString();
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNumber, null, serialnumber + " " + phoneIMEI, null, null);
            Intent intent2 = new Intent(Activation.this, ReceiveSMS.class);
            startActivity(intent2);
        }
    }

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch(arg0.getId())
        {
            case R.id.btn_confirm_activation:
                send_sms();
                break;

            case R.id.btn_return_main:
                Intent intent1 = new Intent(Activation.this, Paya.class);
                startActivity(intent1);
                break;
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

您的问题在于SendSMS类中的此代码:

Intent intent2=new Intent(Activation.this,ReceiveSMS.class);
startActivity(intent2);

接收者不是活动,因此您会收到例外情况 - 您根本无法将Receiver作为活动启动,它们不兼容。我甚至不认为这些行是必需的,因为如果您的应用正在运行,清单中声明的​​Receiver会自动收到短信。

答案 1 :(得分:0)

更改

android:name=".ReceiveSMS" 

android:name="ReceiveSMS"

此刻,您将自动将包装放在其前面,并带有圆点。 由于它试图找到一个名为ReceiveSMS的Activity,其中没有。