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

4

1 回答 1

0

单独注册您的接收器

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

像这样写代码

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

 <receiver android:name=".NotAlarm" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
    </receiver>
于 2013-04-08T10:02:10.703 回答