2016-04-10 9 views
0

Ich versuche, eine Benachrichtigung zu einem bestimmten Zeitpunkt zu senden, ich suche online eine Lösung, aber Code wird nicht funktionieren.Mein Empfänger empfängt keinen Alarmmanager

Ich Benutzer Alarm Manager mit BroadcastReceiver, aber BroadcastReceiver erhalten nie meinen Alarm. Warum?

Manifest.xml:

<receiver android:name=".AlarmReceiver"/> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 

MainActivity.class:

... 
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    Intent i = new Intent(MainActivity.this, AlarmReceiver.class); 
    PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, i, 0); 

    Calendar c = Calendar.getInstance(); 
    c.set(Calendar.HOUR_OF_DAY, hour);  // hour is an int selected from user 
    c.set(Calendar.MINUTE, minute);   // minute is an int selected from user 
    am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); 

Mein Receiver:

public class AlarmReceiver extends BroadcastReceiver { 


@Override 
public void onReceive(Context context, Intent intent) { 


    Log.d("ALARM RECEIVER", "Alarm received"); 

    // do stuff... 
    } 
} 

--edit - full manifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.mycompany.daniel.diabetegestazionale"> 
<uses-permission android:name="android.permission.CALL_PHONE" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".LoginActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".SignInActivity" 
     android:label="@string/title_activity_signin" 
     android:parentActivityName=".LoginActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.LoginActivity" /> 
    </activity> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar" /> 
    <activity 
     android:name=".InformationActivity" 
     android:label="@string/title_activity_information" 
     android:parentActivityName=".MainActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".AcquiringDataActivity" 
     android:label="@string/title_activity_acquiringData" 
     android:parentActivityName=".MainActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".GraphActivity" 
     android:label="@string/title_activity_graph" 
     android:parentActivityName=".MainActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".NoteActivity" 
     android:label="@string/title_activity_note" 
     android:parentActivityName=".MainActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".MealActivity" 
     android:label="@string/title_activity_meal" 
     android:parentActivityName=".MainActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".PhoneNumbersActivity" 
     android:label="@string/title_activity_phoneNumbers" 
     android:parentActivityName=".MainActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".InfoDetailActivity" 
     android:label="@string/title_activity_information" 
     android:parentActivityName=".InformationActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.InformationActivity" /> 
    </activity> 
    <activity 
     android:name=".RecoveryActivity" 
     android:label="@string/title_activity_recoveryPassword" 
     android:parentActivityName=".LoginActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.LoginActivity" /> 
    </activity> 
    <activity 
     android:name=".QuestionAnswerActivity" 
     android:label="@string/title_activity_recoveryPassword" 
     android:parentActivityName=".RecoveryActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.RecoveryActivity" /> 
    </activity> 
    <activity 
     android:name=".RecoveryActivitySetPassword" 
     android:label="@string/title_activity_recoveryPassword" 
     android:parentActivityName=".QuestionAnswerActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.QuestionAnswerActivity" /> 
    </activity> 
    <activity 
     android:name=".NoteDetailedActivity" 
     android:label="@string/title_activity_note" 
     android:parentActivityName=".NoteActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.NoteActivity" /> 
    </activity> 
    <activity 
     android:name=".WriteNoteActivity" 
     android:label="@string/title_activity_note" 
     android:parentActivityName=".NoteActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.NoteActivity" /> 
    </activity> 
    <activity 
     android:name=".DetailedPhoneNumbersActivity" 
     android:label="@string/title_activity_phoneNumbers" 
     android:parentActivityName=".PhoneNumbersActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.PhoneNumbersActivity" /> 
    </activity> 
    <activity 
     android:name=".ModifyPhoneContactActivity" 
     android:label="@string/title_activity_phoneNumbers" 
     android:parentActivityName=".DetailedPhoneNumbersActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.DetailedPhoneNumbersActivity" /> 
    </activity> 
    <activity 
     android:name=".CreateNewContactActivity" 
     android:label="@string/title_activity_create_contact" 
     android:parentActivityName=".PhoneNumbersActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.PhoneNumbersActivity" /> 
    </activity> 
    <activity 
     android:name=".AllInsertionsActivity" 
     android:label="@string/title_activity_all_insertions" 
     android:parentActivityName=".GraphActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.GraphActivity" /> 
    </activity> 
    <activity android:name=".SearchFoodActivity" 
     android:label="@string/title_activity_search_food" 
     android:parentActivityName=".MealActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.mycompany.daniel.diabetegestazionale.MealActivity" /> 
    </activity> 
</application> 

<service 
    android:name=".AlarmService" 
    android:enabled="true" /> 

    <receiver android:name=".AlarmReceiver"/> 

</manifest> 
+0

posten Sie Ihre ganze Manifest.xml –

+0

Haben Sie den Empfänger in Anwendung Tag platziert? – Blackkara

+0

Platzieren Sie den Empfänger in das Anwendungs-Tag. – Blackkara

Antwort

0

Änderung dieser

</application> <service android:name=".AlarmService" android:enabled="true" /> <receiver android:name=".AlarmReceiver"/>

zu

<service android:name=".AlarmService" android:enabled="true" /> <receiver android:name=".AlarmReceiver"/> </application>