开发者

Create notification from non-activity class

Is it possible to create notification from non-activity class? If so, 开发者_如何学编程how?


pass the context in to the class and then create it normally


class A extends Activity{
//required stuff go here
new B().createDialog(A.this);


} 

other class

class B{

public void createDialog(Context context){
//create your dialog or notification here
}
}


As subspider said above, pass the context into the class and you'll be fine:

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
       */
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜