我想从ContentObserver的onChange()方法中激活一个intent.我正在尝试在发送SMS时运行服务,因此ContentObserver,但
Eclipse给我错误,因为它无法解析“上下文”.下面是我的课程代码.
public class SmsObserver extends ContentObserver { public SmsObserver(Handler handler) { super(handler); // TODO Auto-generated constructor stub } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); // On outgoing SMS,do this Intent update = new Intent(context,UpdateService.class); PendingIntent pendingIntent = PendingIntent.getService(context,update,0); try { pendingIntent.send(); } catch (CanceledException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
解决方法
您是否有某些原因在创建实例时无法将应用程序上下文传递给SmsObserver?
public class SmsObserver extends ContentObserver { private Context context; public SmsObserver(Handler handler,Context context) { super(handler); this.context = context; } }
调用类:
new SmsObserver(handler,getApplicationContext())