Android应用程序后端编程

时间:2015-08-19 03:45:58

标签: android

我正在尝试制作一个事件处理Android应用程序,其中即将发生的事件将出现。我知道前端编程,但我的应用程序需要不断与服务器同步。我在互联网上搜索了很多,但我很困惑,我应该阅读什么来制作这个应用程序的后端。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码在每个时间段打开YOURCLASS activity/服务。这将保证活动/每60秒运行一次。

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class ScheduleCheckService extends Service{
    private Timer timer;
    final  int REFRESH=0;
    Context context;
    private PendingIntent pendingIntent;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    context=this;
    //==============================================

    TimerTask refresher;
    // Initialization code in onCreate or similar:
    timer = new Timer();    
    refresher = new TimerTask() {
        public void run() {
          handler.sendEmptyMessage(0);
        };
    };
    // first event immediately,  following after 60 seconds each
    timer.scheduleAtFixedRate(refresher, 0,1000*60); 
    //=======================================================

}

final Handler handler = new Handler() {


    public void handleMessage(Message msg) {
          switch (msg.what) {
          case REFRESH: 
               //your code here 


              break;
          default:
              break;
          }
      }
    };


     void PendingIntentmethod()
     {
     Intent myIntent = new Intent(context, YOURCLASS.class);        
     pendingIntent = PendingIntent.getActivity(context, 0, myIntent, 0);
     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();


     }
}