触发报警定时器时Android显示Toast

时间:2016-05-17 04:07:46

标签: android

我正在试图找出闹钟定时器的工作方式,这样我就可以在用户选择应用中的预定义时间时触发事件。首先,我只想展示一个祝酒词,这样我就可以清楚地看到应用程序正在运行。但是当我运行应用程序并将时间设置为10秒时,处理我的Intent的类似乎永远不会被调用。

我在Main中使用Log.d,我可以看到单击按钮时它被正确记录。但该事件并未在选定时间启动。

这是单击按钮并在控制台中显示Log.d时触发的功能。

  public void scheduleAlarm()
    {
        Long time = System.currentTimeMillis() + 10000;
        Log.d("logs", "This is running in the main act");
        Intent intentAlarm = new Intent(this, affirmationSchedule.class);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
        Toast.makeText(this, "Alarm Has Been Scheduled", Toast.LENGTH_LONG).show();
    }

这是处理警报时间到来时运行的代码的类

public class affirmationSchedule extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("logs", "This function is running");
        Toast.makeText(context, "this is a toast working.", Toast.LENGTH_LONG).show();
    }
}

Log.d never displays. the toast in this class never displays. 

这让我相信我没有正确地创建我的对象。

这就是我在清单中注册接收器的方式。

 <receiver
        android:name="com.wuno.wunoaffirmations"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="com.wuno.BroadcastReceiver" />
        </intent-filter>
    </receiver>

有什么想法吗?

这可能是相关的,

点击按钮后,原始吐司就消失了。这会在控制台中弹出。

05-16 23:10:11.989 14242-14268/com.wuno.wunoaffirmations E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4015c60

但不是十秒钟。更像是5. alarmManager设置为10秒。

2 个答案:

答案 0 :(得分:0)

  public void scheduleAlarm()
    {
        Calendar cal = Calendar.getInstance();
        Log.d("logs", "This is running in the main act");
        Intent intentAlarm = new Intent(this, affirmationSchedule.class);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, cal + 10000, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
        Toast.makeText(this, "Alarm Has Been Scheduled", Toast.LENGTH_LONG).show();
    }

广播接收器

public class affirmationSchedule extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("logs", "This function is running");
        Toast.makeText(context, "this is a toast so this is working.", Toast.LENGTH_LONG).show();
    }
}

在清单

<receiver
        android:name="com.wuno.affirmationSchedule"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="com.wuno.BroadcastReceiver" />
        </intent-filter>
    </receiver>

答案 1 :(得分:0)

这是我如何在我的项目中使用报警管理器。基本上我从谷歌的应用程序代码中遵循了一些代码。所以在这里。我希望这会对你有所帮助。

如何使用它?好吧,只需创建AlramReciver的实例,然后设置它。

private AlarmReceiver alarmReceiver = new AlarmReceiver();
alramReceiver.setAlram();

这是设置alram接收器的辅助类。

public class AlarmReceiver extends WakefulBroadcastReceiver {
    private static AlarmManager alarmManager;
    private static PendingIntent alarmIntent;
    @Override
    public void onReceive(Context context, Intent intent) {
        /*
         * If your receiver intent includes extras that need to be passed along to the
         * service, use setComponent() to indicate that the service should handle the
         * receiver's intent. For example:
         *
         * ComponentName comp = new ComponentName(context.getPackageName(),
         *      MyService.class.getName());
         *
         * // This intent passed in this call will include the wake lock extra as well as
         * // the receiver intent contents.
         * startWakefulService(context, (intent.setComponent(comp)));
         *
         * In this example, we simply create a new intent to deliver to the service.
         * This intent holds an extra identifying the wake lock.
         */
        Intent service= new Intent(context, AlarmService.class);
        startWakefulService(context,service);
    }

    /**
     *set the alram
     * @param context
     */
    public void setAlarm(Context context){
        alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        alarmIntent =PendingIntent.getBroadcast(context,0,intent,0);
        alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 1000 * 60, alarmIntent);

        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

    /**
     * cancels the alram
     * @param context
     */
    public void cancelAlarm(Context context){
        // If the alarm has been set, cancel it.
        if (alarmManager!= null) {
            alarmManager.cancel(alarmIntent);
        }
        // Disable {@code SampleBootReceiver} so that it doesn't automatically restart the
        // alarm when the device is rebooted.
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }
}

这是在设备关闭并再次打开时使用的bootReceiver类

public class BootReceiver extends BroadcastReceiver {
    AlarmReceiver alarmReceiver = new AlarmReceiver();
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            alarmReceiver.setAlarm(context);
        }
    }
}

这是意向服务类,您必须为您的应用编写逻辑。

公共类AlarmService扩展了IntentService {

public AlarmService() {
    super("AlarmService");
}

@Override
protected void onHandleIntent(Intent intent) {
    //Write the logice here
    AlarmReceiver.completeWakefulIntent(intent); // this tell if the related work is complete then system tracks for another alram.
}

最后,您必须在清单中进行更改。

    <service android:name="AlarmService" />
    <receiver android:name="AlarmReceiver" />
    <receiver android:name="BootReceiver" />

我希望这会帮助你至少。 P.s你不必两次发布同样的问题。