Unable to start ServiceIntent android

Posted by Mj1992 on Stack Overflow See other posts from Stack Overflow or by Mj1992
Published on 2012-07-07T20:31:54Z Indexed on 2012/07/07 21:15 UTC
Read the original article Hit count: 443

Filed under:

I don't know why my service class is not working although it was working fine before.

I've the following service class.

public class MyIntentService extends IntentService {

private static PowerManager.WakeLock sWakeLock;
    private static final Object LOCK = MyIntentService.class;


public MyIntentService() 
{
    super("MuazzamService");
}

@Override
protected void onHandleIntent(Intent intent) {

     try 
     {
            String action = intent.getAction();      <-- breakpoint

            if (action.equals("com.google.android.c2dm.intent.REGISTRATION"))   <-- passes by
            {
                handleRegistration(intent);
            }
            else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) 
            {
                handleMessage(intent);
            }
        }
        finally
        {
            synchronized(LOCK)
            {
                sWakeLock.release();
            }
        }
}

private void handleRegistration(Intent intent) 
{
      try
      {
        String registrationId = intent.getStringExtra("registration_id");
        String error = intent.getStringExtra("error");
        String unregistered = intent.getStringExtra("unregistered");       
        if (registrationId != null)
        {
            this.SendRegistrationIDViaHttp(registrationId);
            Log.i("Regid",registrationId);
        }

        if (unregistered != null)
        {} 

        if (error != null) {
            if ("SERVICE_NOT_AVAILABLE".equals(error)) 
            {
               Log.e("ServiceNoAvail",error);
            } 
            else 
            {
                Log.i("Error In Recieveing regid", "Received error: " + error);
            }
        }
      }
      catch(Exception e)
      {
          Log.e("ErrorHai(MIS0)",e.toString());
          e.printStackTrace();
      }
}

private void SendRegistrationIDViaHttp(String regID) {

HttpClient httpclient = new DefaultHttpClient();
     try
     {

        HttpGet httpget = new HttpGet("http://10.116.27.107/php/GCM/AndroidRequest.php?registrationID="+regID+"&[email protected]");    //test purposes k liye muazzam
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity=response.getEntity();
        if(entity!=null)
        {
              InputStream inputStream=entity.getContent();
              String result= convertStreamToString(inputStream);
              Log.i("finalAnswer",result);

     //       Toast.makeText(getApplicationContext(),regID, Toast.LENGTH_LONG).show();

        }
    }
    catch (ClientProtocolException e) 
    {
        Log.e("errorhai",e.getMessage());
        e.printStackTrace();
    }
    catch (IOException e) 
    {
        Log.e("errorhai",e.getMessage());
        e.printStackTrace();
    }
}
 private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        Log.e("ErrorHai(MIS)",e.toString());
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.e("ErrorHai(MIS2)",e.toString());
            e.printStackTrace();
        }
    }
    return sb.toString();
}

private void handleMessage(Intent intent) {
    try {
    String score = intent.getStringExtra("score");
    String time = intent.getStringExtra("time");
    Toast.makeText(getApplicationContext(), "hjhhjjhjhjh", Toast.LENGTH_LONG).show();
    Log.e("GetExtraScore",score.toString());
    Log.e("GetExtratime",time.toString());
    }
    catch(NullPointerException e)
    {
        Log.e("je bat",e.getMessage());
    }
 }

static void runIntentInService(Context context,Intent intent){
    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, MyIntentService.class.getName());
        context.startService(intent);
}

 }

and here's how I am calling the service as mentioned in the android docs.

 Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
 registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
 registrationIntent.putExtra("sender",Sender_ID);
 startService(registrationIntent);

I've declared the service in the manifest file inside the application tag.

<service android:name="com.pack.gcm.MyIntentService"  android:enabled="true"/>

I placed a breakpoint in my IntentService class but it never goes there.But if I declare my registrationIntent like this

Intent registrationIntent = new Intent(getApplicationContext(),com.pack.gcm.MyIntentService);

It works and goes to the breakpoint I've placed but intent.getAction() contains null and hence it doesn't go into the if condition placed after those lines.

It says

 07-08 02:10:03.755: W/ActivityManager(60): Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTER (has extras) }: not found

in the logcat.

© Stack Overflow or respective owner

Related posts about android