1

我正在使用自定义日期和时间创建通知服务,但我不能这样做。我下载了这个代码http://blog.blundell-apps.com/notification-for-a-user-chosen-time/并且工作正常,但是当我在我的项目中包含代码时不起作用。

我有这个代码

package com.blundell.tut.service.task;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.blundell.tut.service.NotifyService;

/**
 * Set an alarm for the date passed into the constructor
 * When the alarm is raised it will start the NotifyService
 * 
 * This uses the android build in alarm manager *NOTE* if the phone is turned off this alarm will be cancelled
 * 
 * This will run on it's own thread.
 * 
 * @author paul.blundell
 */
public class AlarmTask implements Runnable{
    // The date selected for the alarm
    private final Calendar date;
    // The android system alarm manager
    AlarmManager am;
    // Your context to retrieve the alarm manager from
    private final Context contextt;

    public AlarmTask(Context context, Calendar date) {
        Log.v("AlarmTask", "AlarmTask");
        this.contextt = context;
        this.am = (AlarmManager) context.getSystemService(contextt.ALARM_SERVICE);
        this.date = date;
    }

    public void run() {
        Log.v("AlarmTask", "run");
        // Request to start are service when the alarm date is upon us
        // We don't start an activity as we just want to pop up a notification into the system bar not a full activity
        Intent intent = new Intent(contextt, NotifyService.class);
        intent.putExtra(NotifyService.INTENT_NOTIFY, true);
        PendingIntent pendingIntent = PendingIntent.getService(contextt, 0, intent, 0);

        // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
        am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
    }
}

在这一行我得到一个错误

this.am = (AlarmManager) context.getSystemService(contextt.ALARM_SERVICE);

我从这个类发送de context

package com.blundell.tut.service;

import java.util.Calendar;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.blundell.tut.service.task.AlarmTask;

public class ScheduleService extends Service {

    /**
     * Class for clients to access
     */
    public class ServiceBinder extends Binder {
        ScheduleService getService() {
            return ScheduleService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    // This is the object that receives interactions from clients. See
    private final IBinder mBinder = new ServiceBinder();

    /**
     * Show an alarm for a certain date when the alarm is called it will pop up a notification
     */
    public void setAlarm(Calendar c) {
        // This starts a new thread to set the alarm
        // You want to push off your tasks onto a new thread to free up the UI to carry on responding
        new AlarmTask(this, c).run();
    }
}

我得到这个错误

java.lang.nullpointerexception 
   at android.content.contextwrapper.getsystemservice(Contextwrapper.java:386)

对不起我的英语不好 :)

4

2 回答 2

0

如果您只想在未来的某个时间点发出通知,那么您只需要使用AlarmManager、设置 aBroadcastReceiverIntentService从中开始。

以下是一些代码片段:

如果应用程序休眠大约 1 天,则第二天不会调用 AlarmManager

于 2013-02-06T15:04:18.357 回答
0

在这一行:

this.am = (AlarmManager) context.getSystemService(contextt.ALARM_SERVICE);

您应该以静态方式访问 ALARM_SERVICE,请尝试以下操作:

this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

此外,您的调用上下文应该可以通过this.getBaseContext()或访问this.getApplicationContext

即你可以写:

this.am = (AlarmManager) this.getBaseContext.getSystemService(Context.ALARM_SERVICE);
于 2013-02-06T15:05:12.627 回答