我想从我的ContentObserver的onChange()方法中激发一个意图。我试图让服务在发送短信时运行,因此出现了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, 0, update, 0);
try {
pendingIntent.send();
} catch (CanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}发布于 2012-01-12 23:21:12
在创建实例时,是否有什么原因不能直接将应用程序上下文传递到SmsObserver中?
public class SmsObserver extends ContentObserver {
private Context context;
public SmsObserver(Handler handler, Context context) {
super(handler);
this.context = context;
}
}调用类:
new SmsObserver(handler, getApplicationContext())https://stackoverflow.com/questions/8829344
复制相似问题