闹钟管理器的调度程序在重启后无法正常工作

3

这是我在 SO 上的第一个问题。我正在开发一款 Android 应用程序,它将通过生日计算自动设定闹钟时间,并为每个预定日期设定一个共同的闹钟时间。

    c8.add(Calendar.MONTH, 18);
    sdf = new SimpleDateFormat("yyyy-dd-MM");
    //String output = sdf.format(c.getTime());
    Date eighteendtmonth= new Date(c8.getTimeInMillis());
    dteighteenmonth = sdf.format(eighteendtmonth);
    System.out.println("Eighteen Month date is--->"+dteighteenmonth);

    c8.set(Calendar.HOUR_OF_DAY, bhour);
    c8.set(Calendar.MINUTE, bminute);

    try {
        Date date1 = df.parse(dteighteenmonth);        //birthdate
        Date date2 = df.parse(currentdate);        //
        if(date1.equals(date2) || date1.after(date2))
        {
            setAlarm(c8, eighteenmonth, dteighteenmonth, alarmtime, name );
        }

    } catch (ParseException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

    private void setAlarm(Calendar targetCal,  String detail, String vdate, String vtime, String childname){

         alarmid =(int) System.currentTimeMillis();

        System.out.println("vdate is--->"+vdate);
        System.out.println("alarm time is--->"+vtime);
        System.out.println("Alarm is set----->");
        Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
        intent.setAction("android.intent.action.MAIN");
        intent.putExtra("detail", detail);
        intent.putExtra("childname", name);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), alarmid, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);



}

Broadcastreceiver...

       public void onReceive(Context context, Intent intent) {

        Intent pushIntent = new Intent(context, Childlist.class);
        pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startService(pushIntent);

Toast.makeText(context, detail , Toast.LENGTH_LONG).show();

Toast.makeText(上下文,细节,持续时间).show();

AndroidManifest.xml

AndroidManifest.xml

 <uses-permission android:name='android.permission.WAKE_LOCK'/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
 <receiver
                android:name=".AlarmReceiver"
                android:enabled="true"
                android:exported="true"
                android:label="AlarmReceiver">
            <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
 <receiver android:name=".AlarmReceiver" android:process=":remote" />

所以我的问题是,我的调度程序完美地运行。 但在重新启动设备后,没有一个闹钟起作用。 帮帮我:))

请删除android:process=:remote,因为它是不必要的并且浪费资源。 - CommonsWare
2个回答

9
为了实现这一功能,我们需要一个广播接收器来接收“BOOT_COMPLETED”广播。我们必须知道当设备完成启动后,Android系统会发送“BOOT_COMPLETED”广播。
在Android清单文件中注册BootReceiver。
               <receiver android:name=".BootReceiver">
                    <intent-filter>
                            <action android:name="android.intent.action.BOOT_COMPLETED" />
                            <category android:name="android.intent.category.HOME" />
                    </intent-filter>
             </receiver>

不要忘记在清单文件中添加以下权限。
           <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

需要此权限才能监听/处理 BOOT_COMPLETED 操作。

BootReceiver.java

   public class BootReceiver extends BroadcastReceiver
   {

        public void onReceive(Context context, Intent intent)
        {

               // Your code to execute when Boot Completd
                **Schedule your Alarm Here**
                 Toast.makeText(context, "Booting Completed", Toast.LENGTH_LONG).show(); 

        }

   }

BootReceiver的onRecieve()方法将在启动完成时执行,因此我们需要在onRecieve()方法内编写代码。


0

在重启时,警报会被清除。您可以通过将它们存储在数据库中或以某种方式进行序列化,并使用带有 <action android:name="android.intent.action.BOOT_COMPLETED" /> 操作的 BroadcastReceiver,在启动时重新安排它们,以启动设置警报的服务。


@user2226571: "如何从数据库启动闹钟...有什么提示吗?" -- 你可以像设置闹钟一样进行操作。 - CommonsWare
@CommonsWare 是的,我知道我在这个项目中有数据库类。但是如何在手机重启后启动服务?我的代码在设备重启前都能正常工作... - Utpal Ruparel
@CommonsWare 我已经在启动时再次运行了那段代码。但它是在活动中的。您能否建议我如何从BroadcastReceiver启动该活动? - Utpal Ruparel
2
@UtpalRuparel:将代码从活动中移动到其他位置,可以让活动和接收器都可以访问。静态方法通常是一个典型的解决方案。 - CommonsWare
搞定了..我已经将那段代码从活动移动到服务中,该服务在广播接收器启动时启动。现在它运行得很好。感谢@CommonsWare..如何评价您的指导回答.. :) - Utpal Ruparel
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接