用于在指定时间每天创建通知的服务

时间:2013-12-11 07:55:41

标签: android android-notifications

我希望我的应用程序每天早上6点左右显示一条带有“GO MORNING”消息的通知。正如我读到的,为此我需要应用程序在后台运行,所以我需要使用服务。

我尝试过以下代码但是我被卡住了。

MainActivity.java

public void onClickStartService(View v)
    {
        startService(new Intent(this,MyService.class));
    }

    public void onClickStopService(View v)
    {
        stopService(new Intent(this,MyService.class));
    }

和MyService.java是

public class MyService extends Service{

    private static final String TAG = "MyService";

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
    //Note: You can start a new thread and use it for long background processing from here.
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
    }

}

我有按钮来启动和停止服务,它的工作原理。现在我想要服务创建通知,就像我在帖子开头提到的那样。我怎么能这样做?

2 个答案:

答案 0 :(得分:4)

要在特定时间启动服务,我建议您创建一个由警报触发的BroadcastReceiver,然后启动您的服务。

首先写一个这样的BroadcastReceiver:

public class AlarmReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(final Context context, final Intent intent) {
        context.startService(new Intent(context, MyService.class));
    }


    /**
     * Schedule the next update
     * 
     * @param context
     *            the current application context
     */
    private static void scheduleServiceUpdates(final Context context) {
        // create intent for our alarm receiver (or update it if it exists)
        final Intent intent = new Intent(context, AlarmReceiver.class);
        final PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        // compute first call time 1 minute from now
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 10); 
        long trigger = calendar.getTimeInMillis();

        // set delay between each call : 24 Hours
        long delay = 24 * 60 * 60 * 1000;

        // Set alarm
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC, trigger, delay, pending);
        // you can use RTC_WAKEUP instead of RTC to wake up the device
    }
}

然后,您只需调用scheduleServiceUpdate方法即可启动reccuring事件。如果您只使用RTC类型,那么如果在警报触发服务时手机被锁定,它将不会等待,直到用户解锁设备。如果您使用RTC_Wakeup,服务将在给定的时间准确启动。

请注意,AlarmManager中还有其他方法可以触发事件。

答案 1 :(得分:3)

您可以从PendingIntentAlarmManager

开始

Tutorial here

别忘了添加使用

取消闹钟管理器的可能性
mAlarmManager.cancel(pendingIntent);

此外,如果您想按计划启动服务,您可能需要拦截android.intent.action.BOOT_COMPLETED事件以使应用程序在重新启动后立即启动。

相关问题