0

Вот моя проблема: у меня есть служба, запущенная при загрузке или при запуске приложения, эта служба запускает будильник, который загружает файл каждые x минут. Проблема в том, что приемник вещания, похоже, ничего не получает.Широковещательный приемник в службе не принимает сигнал тревоги

здесь заинтересованный код:

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Intent alarmIntent = new Intent(this, ServiceCalendrier.class); 
     pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 

     Toast.makeText(this, "My Service Started ", Toast.LENGTH_LONG).show(); 
     startAlarm(); 

     return Service.START_NOT_STICKY; 
    } 



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

     manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent); 
     Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show(); //this toast is printed 
    } 

    private final BroadcastReceiver receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      getIcs(arg0);// download function 
      Toast.makeText(arg0, "getICS", Toast.LENGTH_LONG).show();// this one doesn't appear 
     } 

    }; 

Должен ли я объявить свою службу в качестве приемника в моем AndroidManifest?

+1

Вы создаете экземпляр generic 'BroadcastReceiver', но вы не регистрируете его или не указываете« IntentFilter »- даже если зарегистрированная ОС не будет знать, что« приемник »прослушивает. – Squonk

ответ

1

я в конце концов удалось заставить его работать.

public void startAlarm() { 
     manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     int interval =5000;//7200000; 
     IntentFilter myFilter = new IntentFilter("WhatEverYouWant"); 

     Intent alarmIntent = new Intent("WhatEverYouWant"); 
     pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 
     registerReceiver(receiver, myFilter); 

     manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent); 
     Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show(); 
    } 

androidManifest:

<service android:enabled="true" android:name="MyService"> 
      <receiver android:name="MyService"> 
       <intent-filter> 
        <action android:name="WhatEverYouWant" /> 
       </intent-filter> 
      </receiver> 
     </service> 
    </application> 

Я до сих пор есть некоторые работы, чтобы понять, как это работает, и очистить свой код, но большое спасибо за вашу помощь

0

Вы должны объявить службу в manidest файл как этот

<application> 
</activity> 
........ 
.......... 
</activity> 
     <service android:name=".ServiceNameClass"></service> 
    </application> 

также и необходимо зарегистрировать ваш широковещательный somethink как этот

LocalBroadcastManager.getInstance(getBaseContext()).registerReceiver(mMessageReceiver,new IntentFilter("my-event")); 
+0

Итак, моя служба хорошо провозглашена. – Slowmotion

+0

Вам необходимо зарегистрировать свою трансляцию в сервисе, как и выше – sakir

Смежные вопросы