Android : Get Notification from Asynctask process

on Thursday, September 11, 2014


I need to send notification from underground process. in this project i tried to send a simple notification 10 seconds after 10seconds. it runs but don't send me notification!



public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Long previousDate = new Long(System.currentTimeMillis());
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
new GetMethodEx1().execute(previousDate);


}



//this is the method that send notifications


}




class GetMethodEx1 extends AsyncTask<Long,Void,Void>{

private Context mContext;
private int NOTIFICATION_ID = 1;
private Notification mNotification;
private NotificationManager mNotificationManager;





@Override
protected Void doInBackground(Long... params){
Long previousDate = params[0];
Long Intervall = new Long(System.currentTimeMillis());
Boolean a = false;
while ( a = false){
if ((Intervall - previousDate) > 10000 ){


String c = "ciao";
String b = "ciai";
//sendNotification()
createNotification( c, b);


previousDate = Intervall;



}


}
return null;

}





private void createNotification(String contentTitle, String contentText) {

//Build the notification using Notification.Builder
Notification.Builder builder = new Notification.Builder(mContext)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setAutoCancel(true)
.setContentTitle(contentTitle)
.setContentText(contentText);

//Get current notification
mNotification = builder.getNotification();

//Show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}


it don't crash but don't work!


0 comments:

Post a Comment