重启后不会重置警报

时间:2013-04-08 09:54:30

标签: android reboot alarms

我开发了一个新的应用程序,显示电视节目的时间,我想为这个时间做提醒..这工作正常但重启后所有警报都被删除然后我使用接收器BOOT_COMPLETED动作来检索数据来自database,并在重新启动后直接重置所有alarms,但它不起作用

这是我的代码:

的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();

    }

}

并且有些跟踪,因为您注意到我将Toast放在 BootCompletedReceiver.java NotAlarm.java

重新启动后会显示第一个Toast,但不会

1 个答案:

答案 0 :(得分: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>