我有开始在启动一个AsyncTask的(通过服务调用)。在某些情况下,我想送一个通知,开始主要活动。我的问题是当我尝试调用:
I have an AsyncTask which starts on boot (called by a service). In some cases I would like to send a notification to start the main Activity. My problem comes when I try to call:
NotificationManager notManager = (NotificationManager) getSystemService(ns);
在这里日食表明了我一个错误,因为AsyncTask的还没有得到getSystemService方法。任何想法?
where eclipse shows me an error because AsyncTask hasn't got getSystemService method. Any idea?
感谢您。
由于getSystemService是Context类的方法。检查出来的这里。
Because getSystemService is a method of Context class. Check it out here.
在调用该函数,创建一个在您的AsyncTask一个上下文变量。然后,它在构造函数,即初始化。
Before you call the function, create a Context variable in your AsyncTask. Then initialize it in your constructor, i.e.
Context context;
public MyAsyncTask(Context contextin)
{ context = contextin;}
然后用这个上下文变量调用你的函数:
Then use this context variable to call your function:
NotificationManager notManager = (NotificationManager) context.getSystemService(ns);
当您正在执行的AsyncTask不要忘了输入上下文:
Do not forget to input your context when you are executing AsyncTask:
MyAsyncTask mytask = new MyAsyncTask(getApplicationContext());
mytask.execute();
我也有一种感觉,一种IntentService会更适合您的需求。
I also have a feeling that an IntentService would be more suitable for your needs.