Unable to instantiate service on adding intent.setClassName(context, ProcessIntent.class.getName()); <p>I am developing an application which uses Google Cloud Messaging. I am referring to this link to start making the application. </p> <blockquote> <p><a href="http://developer.android.com/google/gcm/gcm.html" rel="nofollow">http://developer.android.com/google/gcm/gcm.html</a></p> </blockquote> <p>But when I enter the line intent.setClassName(context, ProcessIntent.class.getName()); in ProcessIntent.java class, I get a "Could not instantiate service com.venky.ProcessIntent" error. The java code is below.</p> <pre><code> package com.venky.gcmexample; import android.app.IntentService; import android.content.Context; import android.content.Intent; import android.os.PowerManager; import android.util.Log; import android.widget.Toast; public class ProcessIntent extends IntentService { public ProcessIntent(String name) { super(name); } private static PowerManager.WakeLock sWakeLock; private static final Object LOCK = ProcessIntent.class; private static final String TAG = "ProcessIntent"; @Override protected void onHandleIntent(Intent intent) { try { String action = intent.getAction(); if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) { handleRegistration(intent); } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) { handleMessage(intent); } } finally { synchronized(LOCK) { sWakeLock.release(); } } } private void handleMessage(Intent intent) { } private void handleRegistration(Intent intent) { String registrationId = intent.getStringExtra("registration_id"); String error = intent.getStringExtra("error"); String unregistered = intent.getStringExtra("unregistered"); // registration succeeded if (registrationId != null) { // store registration ID on shared preferences // notify 3rd-party server about the registered ID Toast.makeText(this, registrationId, Toast.LENGTH_SHORT).show(); Log.d("handleRegistration", "got registration Id"); } // unregistration succeeded if (unregistered != null) { // get old registration ID from shared preferences // notify 3rd-party server about the unregistered ID Toast.makeText(this, registrationId, Toast.LENGTH_SHORT).show(); Log.d("handleRegistration", "un registration Id"); } // last operation (registration or unregistration) returned an error; if (error != null) { if ("SERVICE_NOT_AVAILABLE".equals(error)) { // optionally retry using exponential back-off // (see Advanced Topics) Toast.makeText(this, error, Toast.LENGTH_SHORT).show(); Log.d("handleRegistration", error); } else { // Unrecoverable error, log it Toast.makeText(this, error, Toast.LENGTH_SHORT).show(); Log.d("handleRegistration", error); Log.i(TAG, "Received error: " + error); } } } static void processIntent(Context context, Intent intent){ Toast.makeText(context, "Intent Service started successfully", Toast.LENGTH_SHORT).show(); Log.d("ProcessIntent", "Intent Service started successfully"); synchronized(LOCK) { if (sWakeLock == null) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_wakelock"); } } sWakeLock.acquire(); intent.setClassName(context, ProcessIntent.class.getName()); context.startService(intent); Toast.makeText(context, "reached end of processIntent", Toast.LENGTH_SHORT).show(); } } </code></pre> <p>There are two other java files as shown below and also the manifest. MyBroadcastReceiver.java :</p> <pre><code>package com.venky.gcmexample; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.Toast; public class MyBroadcastReceiver extends BroadcastReceiver { @Override public final void onReceive(Context context, Intent intent) { Toast.makeText(context, "Recieved a broadcast intent", Toast.LENGTH_SHORT).show(); Log.d("MyBroadcstReceiver", "Recieved an intent broadcast"); ProcessIntent.processIntent(context, intent); setResult(Activity.RESULT_OK, null, null); } } </code></pre> <p>MainActivity.java</p> <pre><code>package com.venky.gcmexample; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; public class MainActivity extends Activity implements OnClickListener{ Button bReg, bUnreg; String senderID = "369107456320"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bReg = (Button) findViewById(R.id.bReg); bUnreg = (Button) findViewById(R.id.bUnreg); bReg.setOnClickListener(this); bUnreg.setOnClickListener(this); } @Override public void onClick(View v) { switch(v.getId()){ case(R.id.bReg): Intent regIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); regIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); regIntent.putExtra("sender", senderID); Toast.makeText(this, "Registration Started", Toast.LENGTH_SHORT).show(); Log.d("Main Activity bReg", "Registration Started"); startService(regIntent); break; case(R.id.bUnreg): Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER"); unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); Toast.makeText(this, "Unregistration Started", Toast.LENGTH_SHORT).show(); Log.d("Main Activity bUnreg", "Unregistration Started"); startService(unregIntent); } } } </code></pre> <p>AndroidManifest.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.venky.gcmexample" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" /&gt; &lt;uses-permission android:name="android.permission.INTERNET"/&gt; &lt;uses-permission android:name="android.permission.WAKE_LOCK"/&gt; &lt;uses-permission android:name="android.permission.GET_ACCOUNTS"/&gt; &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/&gt; &lt;permission android:name="com.venky.gcmexample.permission.C2D_MESSAGE" android:protectionLevel="signature" /&gt; &lt;uses-permission android:name="com.venky.gcmexample.permission.C2D_MESSAGE" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.venky.gcmexample.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;receiver android:name="com.venky.gcmexample.MyBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" &gt; &lt;intent-filter&gt; &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt; &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt; &lt;category android:name="com.venky.gcmexample" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;service android:name=".ProcessIntent" /&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>The error log is :</p> <pre><code>03-27 13:24:50.405: D/WindowManagerImpl(20132): addView, new view, mViews[0]: com.android.internal.policy.impl.PhoneWindow$DecorView@40793c90 03-27 13:24:55.400: D/View(20132): onTouchEvent: viewFlags: 0x18004001 03-27 13:24:55.400: D/View(20132): onTouchEvent: isFocusable: true, isFocusableInTouchMode: false, isFocused: false; focusTaken: false 03-27 13:24:55.420: D/Main Activity bReg(20132): Registration Started 03-27 13:24:55.450: D/WindowManagerImpl(20132): addView, new view, mViews[1]: android.widget.LinearLayout@4079e3c0 03-27 13:24:57.431: D/WindowManagerImpl(20132): finishRemoveViewLocked, mViews[1]: android.widget.LinearLayout@4079e3c0 03-27 13:24:59.413: D/MyBroadcstReceiver(20132): Recieved an intent broadcast 03-27 13:24:59.423: D/ProcessIntent(20132): Intent Service started successfully 03-27 13:24:59.443: D/WindowManagerImpl(20132): addView, new view, mViews[1]: android.widget.LinearLayout@407a1cb0 03-27 13:24:59.453: D/dalvikvm(20132): newInstance failed: no &lt;init&gt;() 03-27 13:24:59.453: D/AndroidRuntime(20132): Shutting down VM 03-27 13:24:59.453: W/dalvikvm(20132): threadid=1: thread exiting with uncaught exception (group=0x4028f5a0) 03-27 13:24:59.463: E/AndroidRuntime(20132): FATAL EXCEPTION: main 03-27 13:24:59.463: E/AndroidRuntime(20132): java.lang.RuntimeException: Unable to instantiate service com.venky.gcmexample.ProcessIntent: java.lang.InstantiationException: com.venky.gcmexample.ProcessIntent 03-27 13:24:59.463: E/AndroidRuntime(20132): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2287) 03-27 13:24:59.463: E/AndroidRuntime(20132): at android.app.ActivityThread.access$2500(ActivityThread.java:135) 03-27 13:24:59.463: E/AndroidRuntime(20132): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1118) 03-27 13:24:59.463: E/AndroidRuntime(20132): at android.os.Handler.dispatchMessage(Handler.java:99) 03-27 13:24:59.463: E/AndroidRuntime(20132): at android.os.Looper.loop(Looper.java:150) 03-27 13:24:59.463: E/AndroidRuntime(20132): at android.app.ActivityThread.main(ActivityThread.java:4389) 03-27 13:24:59.463: E/AndroidRuntime(20132): at java.lang.reflect.Method.invokeNative(Native Method) 03-27 13:24:59.463: E/AndroidRuntime(20132): at java.lang.reflect.Method.invoke(Method.java:507) 03-27 13:24:59.463: E/AndroidRuntime(20132): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 03-27 13:24:59.463: E/AndroidRuntime(20132): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 03-27 13:24:59.463: E/AndroidRuntime(20132): at dalvik.system.NativeStart.main(Native Method) 03-27 13:24:59.463: E/AndroidRuntime(20132): Caused by: java.lang.InstantiationException: com.venky.gcmexample.ProcessIntent 03-27 13:24:59.463: E/AndroidRuntime(20132): at java.lang.Class.newInstanceImpl(Native Method) 03-27 13:24:59.463: E/AndroidRuntime(20132): at java.lang.Class.newInstance(Class.java:1409) 03-27 13:24:59.463: E/AndroidRuntime(20132): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2284) 03-27 13:24:59.463: E/AndroidRuntime(20132): ... 10 more </code></pre> <p>The error goes away when I comment out the line</p> <blockquote> <p>intent.setClassName(context, ProcessIntent.class.getName());</p> </blockquote> <p>But the application the doesnot show the registration ID toast from the onHandleIntent() method.</p>