从非活动类创建通知

问题描述:

是否可以从非活动类创建通知?如果是这样,怎么样?从非活动类创建通知

通过上下文中的类,然后创建它通常

+0

我不知道你的意思,你可以发布一些片段吗?谢谢 – Waypoint

+0

@Hmyzak对不起,下次我会尝试示例:(希望你罚款 – PedroAGSantos

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 
} 
} 

正如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 
     */ 
    } 
}