2天前重新啓動後,我曾問這樣的事情My Question 3 days Ago。現在我有一個新問題。請參閱下面的代碼。重新啓動AlarmManager使用BootService
我
public void startAlarm(final Context context){
myRef.child("users").child(uId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot babyDataSnapshot) {
for (DataSnapshot babySnapshot: babyDataSnapshot.getChildren()) {
final Baby baby = babySnapshot.getValue(Baby.class);
uBabyName = baby.getuBabyName();
myRef.child("users").child(uId).child(uBabyName).child("Vaccines").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot vaccineDataSnapshot) {
for (DataSnapshot vaccineSnapshot: vaccineDataSnapshot.getChildren()){
Vaccine vaccine = vaccineSnapshot.getValue(Vaccine.class);
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH)+1;
day = calendar.get(Calendar.DAY_OF_MONTH);
today = day + "/" + month + "/" + year;
vaccineDate = vaccine.getVaccineDate();
String [] dateParts = vaccineDate.split("/");
int vDay = Integer.parseInt(dateParts[0]);
int vMonth = Integer.parseInt(dateParts[1]);
int vYear = Integer.parseInt(dateParts[2]);
SimpleDateFormat myFormat = new SimpleDateFormat("dd/MM/yyyy");
try {
Date dVaccine = myFormat.parse(vaccineDate);
Date dToday = myFormat.parse(today);
long diffD = calculateDays(dToday, dVaccine);
if(diffD>=0){
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.putExtra("Content", "Waktunya Imunisasi "+ vaccine.getVaccineName() + " untuk " + baby.getuBabyName());
notificationIntent.addCategory("android.intent.category.DEFAULT");
android.icu.util.Calendar cal = android.icu.util.Calendar.getInstance();
PendingIntent broadcast = PendingIntent.getBroadcast(context, NotificationID.getID(), notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
cal.set(android.icu.util.Calendar.YEAR, vYear);
cal.set(android.icu.util.Calendar.MONTH, vMonth-1);
cal.set(android.icu.util.Calendar.DAY_OF_MONTH, vDay);
cal.set(android.icu.util.Calendar.HOUR_OF_DAY, 6);
cal.set(android.icu.util.Calendar.MINUTE, 00);
cal.set(android.icu.util.Calendar.SECOND, 00);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}
else {
//TextView desc = (TextView) findViewById(R.id.desc);
//desc.setText("<0");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
當我把這個在正常的活動,它給我通知startAlarm()方法。但是,當我把這個從BootService
public class BootService extends Service {
@Override
public void onCreate() {
try
{
AddBabyActivity addBabyActivity = new AddBabyActivity();
addBabyActivity.startAlarm(this);
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
它給我一個錯誤,當我重新啓動我的電話
08-14 12:56:19.514 3977-3977/ga.crescere.mom W/System.err: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at com.google.firebase.database.DatabaseReference.child(Unknown Source)
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at ga.crescere.mom.AddBabyActivity.startAlarm(AddBabyActivity.java:160)
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at ga.crescere.mom.BootService.onCreate(BootService.java:19)
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at android.app.ActivityThread.handleCreateService(ActivityThread.java:3216)
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at android.app.ActivityThread.-wrap5(ActivityThread.java)
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1586)
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at android.os.Looper.loop(Looper.java:154)
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6186)
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at java.lang.reflect.Method.invoke(Native Method)
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
08-14 12:56:19.515 3977-3977/ga.crescere.mom W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
08-14 12:56:19.527 3977-4034/ga.crescere.mom D/NetworkSecurityConfig: No Network Security Config specified, using platform default
08-14 12:56:29.603 3977-3997/ga.crescere.mom W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
AddBabyActivity線160
myRef.child("users").child(uId).addValueEventListener(new ValueEventListener() {
BootService線19
addBabyActivity.startAlarm(this);
最後一天,我讓startAlarm靜態而不導入數據庫火力點,它給我重新啓動後,全成通知。但現在,我必須刪除靜態,因爲它會在startAlarm方法所有行給出許多錯誤。有人可以幫我解決這個錯誤嗎?
不,我有它在onCreate方法聲明: '保護無效的onCreate(捆綁savedInstanceState){ 超.onCreate(savedInstanceState); setContentView(R.layout.activity_add_baby); FirebaseUser用戶= FirebaseAuth.getInstance()getCurrentUser(); uId = user.getUid(); }' –
@EMFarih撥打電話之前嘗試的System.out.println(UID),你就會知道,如果它是null.Error說,有空路徑字符串中的孩子() –
真棒,你是對的,。我只需要將uId聲明放在startAlarm()中; ...謝謝.. –