让应用程序在后台运行 [英] keep application running in background

查看:158
本文介绍了让应用程序在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要保持我的应用程序在后台运行
我已经申请把我们定位为服务器
我已经code是这样的:

i want to keep my application running in background
i have application to send our location into server
i have code like this :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);

    updateWithNewLocation(null);

    locationManager.requestLocationUpdates(provider, (10*60*1000), 10,
                                           locationListener);
}
private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status, 
                                Bundle extras){ }
  };
  public void updateWithNewLocation(Location location) {


        if (location != null) {
            Dbhelper helper = new Dbhelper(this);
            final SQLiteDatabase db = helper.getWritableDatabase();
            long time = System.currentTimeMillis();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
            final String curTime = df.format(time);
            final double lat = location.getLatitude();
            final double lng = location.getLongitude();
            final double alt = location.getAltitude();
            System.out.println(lat);
            System.out.println(lng);
            System.out.println(alt);
            db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " +
            "('"+lng+"','"+lat+"','"+alt+"','"+curTime+"')");
            db.close();
            /*Timer timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask(){
                @Override
                public void run(){
                    db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " +
                            "('121.2149012','-6.217837','0.0','2012-05-07 10:20:01')");
                    db.close();
                }
            }, 10*60*1000, 10*60*1000);*/

          } 
       }

我想我的应用程序保持在background..and启动自动运行时,手机上的。

i want my application keep running in the background..and launch automatically when phone on.

推荐答案

有关问题的一个非常简单的答案就是使用的服务。这将允许您执行各种任务,而在背景是,是你最好的赌注的发送您的位置到服务器的悄无声息。

A very simple answer for your problem is to use Service. It will allow you to perform variety of tasks while being in background and is your best bet for sending your location to server silently.

阅读接听求助。

这篇关于让应用程序在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆