I am creating an application in which service is polling a remote
servlet using timer task , I want my service to send notification when
response from server is 1 . Here is my code:
import java.util.Timer;
import java.util.TimerTask;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class Timertask extends Service {
private static final String TAG = Timertask.class.getSimpleName();
NotificationManager nm;
static final int uniqueid = 139868;
private Timer timer;
String response = "test";
private TimerTask updateTask = new TimerTask() {
@Override
public void run() {
Log.i(TAG, "Timer task doing work");
try {
response = CustomHttpClient.executeHttpGet("http://
192.168.1.42:8080/NotifyServlet/serv");
Log.i(TAG, "after response"+response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String res=response.toString();
res= res.replaceAll("\\s+","");
Log.i(TAG, "after res"+res);
if(res=="1")
sendnotification();
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
protected void sendnotification() {
// TODO Auto-generated method stub
Log.i(TAG, "sendnotification doing work");
try
{
nm =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this,test.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
0);
String body = "Hi this is test";
String title = "Ritu";
Notification n = new
Notification(R.drawable.icon,body,System.currentTimeMillis());
n.setLatestEventInfo(this, title, body, pi);
n.defaults = Notification.DEFAULT_LIGHTS;
nm.notify(uniqueid, n);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service creating");
timer = new Timer("Timer Task");
timer.schedule(updateTask,0, 300000);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service destroying");
timer.cancel();
timer = null;
}
}
But i checked logcat , even if i am getting res=1 , notification is
not called . Can anyone please help me to point out where i am wrong.
Thanks .
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en