user1926827 user1926827 - 5 days ago 5
Android Question

Send and receive message error on Android

I am trying write a program, and some parts of it sends and receives messages. The problem is that when I call ReceiveSMS class it gives this error:


android.content.ActivityNotFoundException: Unable to find explicit activity class {ir.hadisoft.paya/ir.hadisoft.paya.ReceiveSMS}; have you declared this activity in your AndroidManifest.xml?


This is my receivesms class:

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();
}
}
}
}


This is my 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>


And this is the sendsms class:

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;
}
}
}

Answer

Your problem lies in this code in your SendSMS class:

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

Receivers are NOT Activities, so you get an Exception - you simply cannot launch a Receiver as an Activity, they're incompatible. I don't even think those lines are needed, because the Receiver declared in the manifest automatically receives the SMS if your app is running.

Comments