是否可以从非活动类创建通知?如果是这样,怎么样?
答案 0 :(得分:4)
将上下文传递给类,然后正常创建
答案 1 :(得分:2)
class A extends Activity{
//required stuff go here
new B().createDialog(A.this);
}
其他班级
class B{
public void createDialog(Context context){
//create your dialog or notification here
}
}
答案 2 :(得分:2)
正如上面提到的subspider,将上下文传递给类,你会没事的:
public class DoSomethingClass {
//Here's a context
private Context _CONTEXT;
//Construct that sets the context
public DoSomethingClass(Context c) {
this._CONTEXT = c;
}
public void createNotification() {
/*
Create the notification as usual, just make sure you alter the following lines:
Intent notificationIntent = new Intent(this, MyClass.class);
Context context = this.getApplicationContext();
^Make sure you alter this into this._CONTEXT above
*/
}
}