Android: AsyncTask with AlarmManager and Service

后端 未结 3 912
清酒与你
清酒与你 2021-01-23 18:55

I want to post JSON string with HttpURLConnection api to the localhost server(WAMP) periodically every 60 seconds to be inserted in the database. Therefore, I am executing MyAs

3条回答
  •  一向
    一向 (楼主)
    2021-01-23 19:51

    first we create a service like this

    public class ChatSevice extends IntentService{
    
        public ChatSevice() {
            super("Some");
        }
    
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
    
            //this is asynk task class
            new ChatConnect(ChatSevice.this).execute();
    
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            // TODO Auto-generated method stub
    
        }
    
    
    
    
    }
    

    and now call this service like this way

    Intent intent3 = new Intent(this, ChatSevice.class);
            PendingIntent pintent3 = PendingIntent.getService(this, 0, intent3, 0);
            AlarmManager alarm3 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            // for 30 mint 60*60*1000
            alarm3.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                    1000, pintent3);
    
            startService(new Intent(getBaseContext(), ChatSevice.class));
    

提交回复
热议问题