Search code examples
androidandroid-intentandroid-studioandroid-serviceandroid-broadcast

Androidmanifest build error about receiver within a service


I'm trying to build an application with gradle>assembleRelease on AndroidStudio but I have an error:

Error:(31) Error: The element must be a direct child of the element [WrongManifestParent]

the concerned code is :

  <service android:enabled="true" android:name="com.example.xx.ServiceCalendar">
        <receiver android:name="com.example.xx.ServiceCalendar">
            <intent-filter>
                <action android:name="ALARM_DL" />
                <action android:name="ALARM_PARSE" />
                <action android:name="ALARM_NOTIF" />
            </intent-filter>
        </receiver>
    </service>

But the problem is if I put my receiver directly under my application, it die when an alarm is set.

What is the best practice about this? Thanks in advance

UPDATE :

I understand that receiver and Services are two differents things, here is a part of my ServiceCalendar class :

public class ServiceCalendar extends Service {
    private PendingIntent pendingIntent;
    private AlarmManager manager;
    BroadcastReceiver mReceiver;


public void startAlarm() {
    manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    int interval =7200000;


    Intent alarmIntent = new Intent("ALARM_DL");
    pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);


    manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);

}

private final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context arg0, Intent arg1) {

        if (arg1.getAction().equals("ALARM_DL"))
        {
            //action 1
        }
        else if (arg1.getAction().equals("ALARM_PARSE")){
             //action 2
        }
        else if(arg1.getAction().equals("ALARM_NOTIF")){
            //action3
        }

    }

};
}

As you can see I'm trying to run a Broadcastreceiver in my service, but i don't know how to set it up in the manifest.

Is there any way to do this (I think so as I've seen some exemples)?


Solution

  • You have to create a Service and a BroadcastReceiver separately and not create an inner dynamic BroadcastReceiver.

    In AndroidManifest:

    <service android:enabled="true" android:name="com.example.xx.ServiceCalendar">
    </service>
    <receiver android:name="com.example.xx.BroadcastReceiverCalendar">
      <intent-filter>
           <action android:name="ALARM_DL" />
           <action android:name="ALARM_PARSE" />
           <action android:name="ALARM_NOTIF" />
      </intent-filter>
    </receiver>
    

    Create 2 classes, 1rst service:

    public class ServiceCalendar extends Service {
        private PendingIntent pendingIntent;
        private AlarmManager manager;
    
        public void startAlarm() {
            manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            int interval =7200000;
    
    
            Intent alarmIntent = new Intent("ALARM_DL");
            pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
    
            manager.setRepeating(AlarmManager.RTC_WAKEUP,        System.currentTimeMillis(), interval, pendingIntent);
        }
    }
    

    And 2nd BroadcastReceiver:

    public class BroadcastReceiverCalendar extends BroadcastReceiver {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
    
            if (arg1.getAction().equals("ALARM_DL"))
            {
                //action 1
            }
            else if (arg1.getAction().equals("ALARM_PARSE")){
                 //action 2
            }
            else if(arg1.getAction().equals("ALARM_NOTIF")){
                //action3
            }
    
        }
    }