如何在android中重新启动后设置所有警报

时间:2013-04-02 13:30:23

标签: android alarm reboot

我正在开发一个应用程序,设置警报以触发通知,让我想起电视节目

现在我存储了有关数据库中警报的数据,但重启后所有警报都被删除

我在接收receiver时使用BOOT_COMPLETED设置闹钟但是当我在其中设置闹钟时,闹钟会触发另一个接收器设置通知

我该怎么做

这是代码:

的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

这些行有错误: -

1 - Intent alarmIntent = new Intent(this,NotAlarm.class);

2 - am =(AlarmManager)getSystemService(Context.ALARM_SERVICE);

package com.shadatv.shada;

import java.util.Calendar;

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

public class BootCompletedReceiver extends BroadcastReceiver {

    public DAOTimes timeDatabase = null;
    AlarmManager am;

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

        if (allNotifs.moveToFirst()) {
            do{
                Intent alarmIntent = new Intent(this, NotAlarm.class);
                String noPrName = allNotifs.getString(allNotifs.getColumnIndex("_noprogram")).toString().trim();
                String noTime = allNotifs.getString(allNotifs.getColumnIndex("_notime")).toString().trim();
                intent.setAction(noTime);
                intent.putExtra("nameSelected", noPrName);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                        new Intent(), PendingIntent.FLAG_CANCEL_CURRENT);
                am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                am.setRepeating(AlarmManager.RTC_WAKEUP, Long.valueOf(noTime),
                        7 * 24 * 60 * 60 * 1000, pendingIntent);
            }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;

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);
//      CharSequence ticker = (CharSequence) (R.string.noTitle);
        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);

    }

}

现在,如何修改此代码才能正确运行?

0 个答案:

没有答案