I want to start my service via Alarm manager. i wrote below code. this code works good when my application to be in background or foreground but when i kill my application (I mean swiping application from recent apps) the program get an error. I don't know what is my problem and how can solve this.
Error:
Caused by: java.lang.NullPointerException at com.company.test.helper.ServiceSchedular.startService(ServiceSchedular.java:74)
I have a class that extends from Application like below:
public class KITILApplication extends Application{
private static Context context;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
ACRA.init(this);
ORMDroidApplication.initialize(this);
KITILApplication.context=getApplicationContext();
}
public static Context getappContext(){
return KITILApplication.context;
}
}
and my ServiceSchedularService for scheduling alarms for starting service is in below:
public class ServiceSchedular {
//Called from my main activity
private ServiceSchedular() {
alarmManager = (AlarmManager) KITILApplication.getappContext().getSystemService(Context.ALARM_SERVICE);
}
private static void startService(Date startDate) {
uniqueIdForStart = (int) Calendar.getInstance().getTimeInMillis();
//PlEASE NOTICE: I checked my application via both of startintent(comment startIntent and startIntent)
Intent startIntent = new Intent(KITILApplication.getappContext(), LocationService.class);
//Intent startIntent = new Intent("com.company.test.START_SERVICE");
PendingIntent startPendingIntent = PendingIntent.getService(KITILApplication.getappContext(), uniqueIdForStart, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//I get error from below line !! Line = 74
alarmManager.set(AlarmManager.RTC_WAKEUP, millisToStartDate, startPendingIntent);
stopService(startDate);
}
public static void checkNextPeriod() {
.
.
startService(new Date(millisDate));
}
}
I have a broadcastReciever that called checkNextPeriod and in checkNextPeriod, i called startService method.
AndroidManifest.xml:
<service android:name=".Service.LocationService">
<intent-filter>
<action android:name="com.company.test.START_SERVICE"></action>
</intent-filter>
</service>
I don't know my problem is for alarmManager object or intent object or application context or another thing. Please help me.