カスタムの日付と時刻で通知サービスを作成していますが、これを行うことができません。このコード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);
このクラスからコンテキストを送信します
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)
私の英語でごめんなさい:)