3 Replies - 759 Views - Last Post: 03 July 2012 - 11:36 AM

#1 pervin26  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-June 12

shows force close in emulator

Posted 03 July 2012 - 06:15 AM

my sms receiver shows force close when run in android emulator 2.3.3.it shows no error in eclipse but when run it shows force close.i don't know where is my mistakes.any correction would be helpful for me.

SMS_ReceiverActivity.java

package pack.coderzheaven;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
 
@SuppressWarnings("deprecation")
public class SMS_ReceiverActivity extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
 
        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage SMS[] = new SmsMessage[messages.length];
        for (int n = 0; n < messages.length; n++) {
            SMS[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
        }
        /* GETTING THE LAST JUST ARRIVED MESSAGE FROM INBOX */
        Toast toast = Toast.makeText(context,"Received SMS inside SMS_Receiver : " + SMS[0].getMessageBody(), Toast.LENGTH_LONG);
        toast.show();
    }
}


SMS_ReceiverManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pack.coderzheaven"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SMS_ReceiverActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>


i attach my logcat file here.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: shows force close in emulator

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 275
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: shows force close in emulator

Posted 03 July 2012 - 07:22 AM

SMS_ReceiverActivity is a BroadcastReciever not an Activity! You need to register in the manifest as a broadcast receiver!
Was This Post Helpful? 1
  • +
  • -

#3 pervin26  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-June 12

Re: shows force close in emulator

Posted 03 July 2012 - 07:51 AM

i already register SMS_ReceiverActivity in manifest.but still the same..can you explain to me?i am beginner to android.
Was This Post Helpful? 0
  • +
  • -

#4 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 275
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: shows force close in emulator

Posted 03 July 2012 - 11:36 AM

You registered it as an activity which would be fine but your class extends BroadcastReciever so you need to register it as a BroadcastReciever!
this is how it should be:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pack.coderzheaven"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
		<receiver android:name=".SMS_ReceiverActivity">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>
    </application>

</manifest>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1