Mat*_*ris 7 android android-context
我想从onChange()我的方法中发出一个意图ContentObserver.我正在尝试在发送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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Jiv*_*ngs 12
是否有一些原因你不能在创建实例时将应用程序上下文传递给SmsObserver?
public class SmsObserver extends ContentObserver {
private Context context;
public SmsObserver(Handler handler, Context context) {
super(handler);
this.context = context;
}
}
Run Code Online (Sandbox Code Playgroud)
调用类:
new SmsObserver(handler, getApplicationContext())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1840 次 |
| 最近记录: |