0

I'm trying to use this very simple Service and BroadcastReceiver but I'm getting a
ClassNotFound exception. If I don't use startService things are fine so the problem it's with the receiver. I have registered it in the android manifest file so why do I get the ClassNotFound exception?

I will be using this method of communication for polling a php file and updating a ListView. Is this the appropriate way of communicating (broadcast intent)?

public class MessengerServiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    startService(new Intent(this,MessengerService.class));

}

class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle b=intent.getExtras();
        if(b==null)
            return;

        String msg=b.getString("message");
        TextView tv=(TextView) findViewById(R.id.textView1);
        tv.setText(msg);
    }

    }
}

public class MessengerService extends Service {


    HttpClient hc;
    HttpPost hp;
    String s[]={"pratik","harsha","dayal","hritika"};


    public MessengerService() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        hc=new DefaultHttpClient();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Thread t=new Thread(new Runnable(){

            public void run()
            {
                    int k=0;
                while(true)
                {   
                    Intent i=new Intent("com.pdd.messenger.MyAction");
                    i.putExtra("message",s[k]);
                    sendBroadcast(i);
                        try 
                        {
                            Thread.sleep(3000);
                        } 

                        catch (InterruptedException e) 
                        {


      Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_LONG)
                                     .show();
                        }
                    k=(k+1)%4;
                }
            }

        });
        t.start();



        return super.onStartCommand(intent, flags, startId);
    }

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pdd.messenger"
android:versionCode="1"
android:versionName="1.0" >

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

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

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

    <receiver android:name=".MyReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="com.pdd.messenger.MyAction"/>
        </intent-filter>
    </receiver>

    <service android:name=".MessengerService"></service>
</application>

</manifest>

Yury
  • 20,618
  • 7
  • 58
  • 86
Pratik
  • 246
  • 1
  • 9
  • I tried this as mentioned here http://stackoverflow.com/questions/4391974/is-it-possible-to-define-a-broadcast-receiver-as-an-inner-class-in-manifest-file but still i m having d same problem – Pratik Mar 07 '12 at 06:21
  • You have to write MyReceiver class as separate class. You included that MyReceiver class as a subclass of MessengerServiceActivity. – Satheeshkumar Mar 07 '12 at 12:05
  • Thx i have done that and now its working but i would want to know is this is best way to communicating from service ?? Actually what i am trying is polling a php file every N seconds and update gui accordingly. – Pratik Mar 07 '12 at 14:48

0 Answers0