尝试从 ContentObserver 激发意图
我想从 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, 0, update, 0);
try {
pendingIntent.send();
} catch (CanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I'd like to fire an intent from the onChange()
method of my ContentObserver
. I'm trying to get a Service to run when an SMS is sent, hence the ContentObserver
, but Eclipse is giving me errors because it cannot resolve "context". Below is my code for the class.
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是否有某种原因无法在创建实例时将应用程序上下文传递到 SmsObserver 中?
调用类:
Is there some reason you cant just pass the application context into the SmsObserver when the instance is created?
Invoking class: