[英]java.lang.nullpointerexception at android.content.contextwrapper.getsystemservice(Contextwrapper.java:386)
I'm creating a Notification Service with custom date and time, and I can't do this. 我正在创建具有自定义日期和时间的Notification Service,但无法执行此操作。 I download this code http://blog.blundell-apps.com/notification-for-a-user-chosen-time/ and this work fine, but when I include the code in my project don't work.
我下载了此代码http://blog.blundell-apps.com/notification-for-a-user-chosen-time/ ,效果很好,但是当我将代码包含在项目中时,该代码不起作用。
I have this code 我有这个代码
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);
}
}
In this line i get a error 在这一行我得到一个错误
this.am = (AlarmManager) context.getSystemService(contextt.ALARM_SERVICE);
I send de context from this class 我从这个班级发送上下文
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();
}
}
and i get this error 我得到这个错误
java.lang.nullpointerexception
at android.content.contextwrapper.getsystemservice(Contextwrapper.java:386)
Sorry for my english :) 对不起我的英语不好 :)
If all you want is to issue a notification at a point in time in the future, all you need is to use AlarmManager
, set up a BroadcastReceiver
and start an IntentService
from that. 如果您只想在将来的某个时间发布通知,则只需使用
AlarmManager
,设置BroadcastReceiver
并IntentService
启动IntentService
。
Here are some code snippets: 以下是一些代码段:
AlarmManager Not called next day if the app sleeps for abt 1 day 如果应用休眠1天,第二天不调用AlarmManager
In this line: 在这一行:
this.am = (AlarmManager) context.getSystemService(contextt.ALARM_SERVICE);
You should access ALARM_SERVICE in a static way, try this instead: 您应该以静态方式访问ALARM_SERVICE,请尝试以下操作:
this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Also your calling context should be accessible via this.getBaseContext()
or this.getApplicationContext
同样,您的调用上下文也可以通过
this.getBaseContext()
或this.getApplicationContext
访问。
ie you could write: 即你可以写:
this.am = (AlarmManager) this.getBaseContext.getSystemService(Context.ALARM_SERVICE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.