Search code examples
androidgoogle-cloud-messagingpackageproject-organization

How to organising your code in Android


How do I organise my code? I have tried to put everything in a different package but then my notifications with GCM doesn't work anymore. if i put everything in the same root package no problem.

this are my two tests, left gives problems on gcm right doesn't:

enter image description here

Is there any way to easy organise your code like I did in the right one? or do all the classes, activities, services needs to be in the same package?

EDIT 1 = SOLUTION

You need to create your own class that extends GCMBroadcastReceiver like this:

import android.content.Context;

import com.google.android.gcm.GCMBroadcastReceiver;

public class GCMReceiver extends GCMBroadcastReceiver{
    @Override
    protected String getGCMIntentServiceClassName(Context context) {
        return "yourAppPackage.yourNewAddedPackage.GCMIntentService";
    }
}

Then you need to change the default receiver in your manifest to the package where this class is located. for example:

<receiver android:name="yourPackage.PackageOfTheGCMReceiver.GCMReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="be.vanlooverenkoen.piautomation" />
            </intent-filter>
        </receiver>

Solution

  • You need to create your own class that extends GCMBroadcastReceiver like this:

    import android.content.Context;
    
    import com.google.android.gcm.GCMBroadcastReceiver;
    
    public class GCMReceiver extends GCMBroadcastReceiver{
        @Override
        protected String getGCMIntentServiceClassName(Context context) {
            return "yourAppPackage.yourNewAddedPackage.GCMIntentService";
        }
    }
    

    Then you need to change the default receiver in your manifest to the package where this class is located. for example:

    <receiver android:name="yourPackage.PackageOfTheGCMReceiver.GCMReceiver"
                android:permission="com.google.android.c2dm.permission.SEND" >
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                    <category android:name="be.vanlooverenkoen.piautomation" />
                </intent-filter>
            </receiver>