Androidmanifest在服务中构建有关接收器的错误

时间:2015-03-09 10:45:53

标签: android android-intent android-studio android-service android-broadcast

我正在尝试在AndroidStudio上使用gradle> assembleRelease构建一个应用程序但是我有一个错误:

错误:(31)错误:元素必须是元素的直接子元素[WrongManifestParent]

相关代码是:

  <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>

但问题是如果我将我的接收器直接置于我的应用程序之下,它会在设置警报时死亡。

关于此的最佳做法是什么? 提前致谢

更新:

我理解接收者和服务是两个不同的东西,这是我的ServiceCalendar类的一部分:

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
        }

    }

};
}

正如您所看到的,我正在尝试在我的服务中运行Broadcastreceiver,但我不知道如何在清单中设置它。

有没有办法做到这一点(我想我见过一些例子)?

1 个答案:

答案 0 :(得分:0)

您必须单独创建服务和BroadcastReceiver,而不是创建内部动态BroadcastReceiver。

在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>

创建2个类,第一个服务:

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);
    }
}

第二届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
        }

    }
}