[英]Create notification from non-activity class
是否可以從非活動類創建通知? 如果是這樣,怎么辦?
將上下文傳遞給類,然后正常創建它
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
}
}
正如上面的子蜘蛛所說,將上下文傳遞給類,您會沒事的:
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
*/
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.