sorry I have a typo in the last line please replace startService
(myNotificationServiceIntent);  with startService(myIntent);

On Jan 26, 11:04 am, nikhil <[email protected]> wrote:
> Hello Guys,
>
> I am using a service in my app. But it doesn't seem to work in sleep
> mode. I have implemented startforeground to prevent it from shutting
> down but it still does. Following is my code.
>
> public class NotificationService extends Service {
>
>         MediaPlayer notifysound;
>         MediaPlayer textsound;
>         Timer myTimer=null;
>         SharedPreferences myPref;
>         NotificationManager mNotificationManager;
>
>         public void onCreate() {
>                 super.onCreate();
>
>                 if(myTimer==null)
>                         myTimer = new Timer();
>
>               mNotificationManager = (NotificationManager)
>               getSystemService(Context.NOTIFICATION_SERVICE);
>               Notification notification = new Notification(R.drawable.icon,
> "MyService",
>                System.currentTimeMillis());
>               notification.flags = Notification.FLAG_AUTO_CANCEL;
>               RemoteViews contentView = new RemoteViews(getPackageName(),
> R.layout.customnotification);
>               contentView.setImageViewResource(R.id.notificationimage,
> R.drawable.icon);
>               contentView.setTextViewText(R.id.notificationtext, "Service
> Started!");
>               notification.contentView = contentView;
>               View view = new View(this);
>               Intent notificationIntent = new Intent(view.getContext
> (),SomeClass.class);
>               PendingIntent contentIntent = PendingIntent.getActivity
> (view.getContext(), 0, notificationIntent, 0);
>               notification.contentIntent = contentIntent;
>               mNotificationManager.notify(1, notification);
>
>              startForeground(1, notification);
>
>         }
>
>         @Override
>         public IBinder onBind(Intent intent) {
>                         return null;
>                 }
>
>         @Override
>         public int  onStartCommand (Intent intent, int flags, int startId)
>         {
>                 startservice();
>                 return START_STICKY;
>         }
>
>         private void startservice() {
>                 myTimer.schedule( new TimerTask() {
>                                 public void run() {
>                                         DoSomething();
>                                 }
>                         }, 0, 5000);
>                 ; }
>
>         protected void DoSomething() {
>                 try
>                 {
>
>                         Some code which makes http request and gets 
> response...
>                 }
>                 catch(Exception ex)
>                 {
>                         ex.printStackTrace();
>                 }
>         }
>
>         public void onDestroy()
>         {
>                 super.onDestroy();
>                 stopservice();
>         }
>         private void stopservice() {
>
>                 if (myTimer != null)
>                         {
>                                 myTimer.cancel();
>                                 myTimer.purge();
>                                 myTimer = null;
>                         }
>         }
>
> }
>
> Code to call this service
>
> Intent myIntent = new Intent(this,NotificationService.class);
> startService(myNotificationServiceIntent);
>
> Please let me know if I am going wrong somewhere....

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

Reply via email to