Android中非activity类调用activity方法

例如需要使用:

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

第一种方法就是使其类变成Activity。

第二种方法便是传入上下文Context。如下:

1 alarmManager = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);

采用第二种方式可避免如下错误:

java.lang.IllegalStateException: System services not available to Activities before onCreate()

猜你喜欢

转载自www.cnblogs.com/ken9527just/p/11539582.html