0

I developed a new app what displaying the times for TV shows and i want to make reminders for this times .. that's working good but after reboot all alarms are deleted then i used receiver with BOOT_COMPLETED action to retreive data from database and reset all alarms after reboot directly but it doesn't work

this is my code :

manifest.xml

<receiver android:name=".NotAlarm" />
        <receiver android:name=".BootCompletedReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

BootCompletedReceiver.java

package com.shadatv.shada;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.widget.Toast;

public class BootCompletedReceiver extends BroadcastReceiver {

    public DAOTimes timeDatabase;
    Cursor allNotifs;
    AlarmManager am;

    @Override
    public void onReceive(Context context, Intent intent) {
        timeDatabase = new DAOTimes(context);       
        allNotifs = timeDatabase.getAllNotifications();                     

        if (allNotifs.moveToFirst()) {
            do{
                String noPrName = allNotifs.getString(allNotifs.getColumnIndex("_noprname")).toString().trim();
                String noTime = allNotifs.getString(allNotifs.getColumnIndex("_notime")).toString().trim();             

                Intent alarmIntent = new Intent(context, NotAlarm.class);               
                alarmIntent.setAction(noTime);
                alarmIntent.putExtra("nameSelected", noPrName);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                        alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
                am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                am.setRepeating(AlarmManager.RTC_WAKEUP, Long.valueOf(noTime),
                        7 * 24 * 60 * 60 * 1000, pendingIntent);

                Toast.makeText(context,
                        "name : " + Long.valueOf(noTime),
                        Toast.LENGTH_LONG).show();
            }while(allNotifs.moveToNext());     
        }
    }
}

NotAlarm.java

package com.shadatv.shada;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class NotAlarm extends BroadcastReceiver {

    NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        NotificationManager nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(context);
        builder.setSmallIcon(android.R.drawable.btn_star);
        builder.setTicker("تذكير");

        Notification notify = builder.getNotification();

        CharSequence title = intent.getExtras().getString("nameSelected");
        CharSequence details = "تذكير - برنامج " + title;

        PendingIntent pending = PendingIntent
                .getActivity(context, 0, new Intent(), 0);
        notify.setLatestEventInfo(context, title, details, pending);

        nm.notify(0, notify);

        Toast.makeText(context,
                "noti : " + title,
                Toast.LENGTH_LONG).show();

    }

}

and for some trace, as you notice i put Toast in BootCompletedReceiver.java and in NotAlarm.java

The first Toast is displayed after reboot but another one isn't

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
eng.ahmed
  • 905
  • 4
  • 16
  • 38

1 Answers1

0

Register your receiver separately

 <receiver android:name=".NotAlarm" />
   <receiver android:name=".BootCompletedReceiver" >
      <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
    </receiver>

Write code like this

 <receiver android:name=".BootCompletedReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
    </receiver>

and

 <receiver android:name=".NotAlarm" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
    </receiver>
ravindra.kamble
  • 1,023
  • 3
  • 11
  • 20
  • i already did that .. they are separated but first i declared it without intent-filter and i don't want to launch it after reboot but it is called by BootCompletedReceiver – eng.ahmed Apr 08 '13 at 10:36