定时发送GPS信息开机启动

主页面
package com.dongqin.cn;

import com.dongqin.cn.service.LocationService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    TextView myTextView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //平板必须添加否则无法联网
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()    
        .detectDiskReads()    
        .detectDiskWrites()    
        .detectNetwork()   // or .detectAll() for all detectable problems     
        .penaltyLog()    
        .build());    
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()    
        .detectLeakedSqlLiteObjects()   
        .detectLeakedClosableObjects() 
        .penaltyLog()
        .penaltyDeath()    
        .build()); 
        setContentView(R.layout.main);
        Intent intent=new Intent();
        intent.setClass(MainActivity.this, LocationService.class);
        startService(intent);
        finish();
        Toast.makeText(this, "开始服务", Toast.LENGTH_SHORT).show();
   }
}
Service页面
package com.dongqin.cn.service;

import com.dongqin.cn.sendinfo.SendInfo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class LocationService extends Service {
SendInfo send;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setClass(LocationService.this, SendInfo.class);
startActivity(i);
}
@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
发送信息页面
package com.dongqin.cn.sendinfo;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.dongqin.cn.R;
import com.dongqin.cn.soap.SendKsoap;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class SendInfo extends Activity {
    TextView myTextView;
    String pcId;
    double latitude;
    double longitude;
    String currentTime;
    String fangshi="";
    //线程使用
    Handler myHandler;
    LocationThread myThread;
    //
    LocationManager locationManager;
    SendKsoap ksoap;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//平板上网必加
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()    
        .detectDiskReads()    
        .detectDiskWrites()    
        .detectNetwork()   // or .detectAll() for all detectable problems     
        .penaltyLog()    
        .build());    
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()    
        .detectLeakedSqlLiteObjects()    
        .detectLeakedClosableObjects()    
        .penaltyLog()    
        .penaltyDeath()    
        .build()); 
        setContentView(R.layout.main);
        finish();
        //开始发送
        //通过系统服务,取得LocationManager对象
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    startSend();
}
public void startSend()
{
        myThread=new LocationThread();
        myHandler=new Handler();
        myHandler.post(myThread);
}
    //获取本身信息
    public void getComputerInfo()
    {
    //指纹
//    String fingerprint=android.os.Build.FINGERPRINT.toString();
    //ID
    pcId=android.os.Build.ID.toString();
    }
   
    //位置信息经纬度
    public void getLocationInfo()
    {
   
    //GPS信号
    Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    //网络GPS信号
    Location locationNet = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    //一个特殊的位置提供接收位置,而不会发起一个位置修复。
    // 消极的提供者 一个特殊的接受位置信息提供者,没有实际初始化一个固定的位置
        Location location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
//        Log.i("--->location1",""+locationGPS);
//        Log.i("--->location2",""+locationNet);
//        Log.i("--->location",""+location);
        if(locationGPS!=null)
        {
        fangshi = "GPS";
// 纬度
latitude = locationGPS.getLatitude();
// 经度
longitude = locationGPS.getLongitude();
        }else{
if (locationNet != null) {
fangshi = "net";
// 纬度
latitude = locationNet.getLatitude();
// 经度
longitude = locationNet.getLongitude();
} else {
if (location != null) {
fangshi = "PASSIVE";
// 纬度
latitude = location.getLatitude();
// 经度
longitude = location.getLongitude();
} else {
latitude = 0;
longitude = 0;
}
}
        }
    }
   
    //当前时间信息
    public void getCurrentTime()
    {
    SimpleDateFormat formatter=new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");     
    Date curDate=new Date(System.currentTimeMillis());//获取当前时间     
    currentTime=formatter.format(curDate); 
    }
   
    //发送消息
    public void sendInfo()
    {
      ksoap=new SendKsoap();
      String test=ksoap.sendInfo(pcId,""+latitude,""+longitude, currentTime);
//      Log.i("--->ksoap",""+test);
    }
   
    //线程每隔几秒调用
    public class LocationThread implements Runnable
    {
@Override
public void run()
{
// 获取本机信息
getComputerInfo();
// 获得当前时间
getCurrentTime();
// 获取经纬度信息
getLocationInfo();
// 发送消息
sendInfo();
//隔4秒钟调用线程
myHandler.postDelayed(myThread, 4000);
}
    }
}
向服务器传值
package com.dongqin.cn.soap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.util.Log;

public class SendKsoap {
//指定webService的命名空间
private static final String namespace="http://DefaultNamespace";
//调用webService中的方法
private String methodName="test";
//wsdl的webService的IP
private String serviceUrl="http://192.168.1.106:9080/axis/service/DataSynMainService.jws?wsdl";
public String sendInfo(String pcId,String latitude,String longitude,String currentTime)
{
// Log.i("---->ksoap","pcID"+pcId+"latitude"+latitude+"longitude"+longitude+"currentTime"+currentTime);
//创建SoapObject对象,并指定webService的命名空间和方法名
SoapObject soapObject=new SoapObject(namespace,methodName);
//WebService方法的参数
soapObject.addProperty("pcId", pcId);
soapObject.addProperty("latitude", latitude);
soapObject.addProperty("longitude", longitude);
soapObject.addProperty("currentTime", currentTime);
//创建SoapSerializationEnvelope对象,并指定webService的版本
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
//设置bodyOut属性
envelope.bodyOut=soapObject;
//AndroidHttpTransport对象,并指定WSDL文档的URL
AndroidHttpTransport ht=new AndroidHttpTransport(serviceUrl);
try{
//调用WebService的方法
ht.debug=true;
ht.call(null, envelope);
if(envelope.getResponse()!=null)
{
//使用getResponse方法获得WebService返回的结果
SoapObject result=(SoapObject)envelope.bodyIn;
//直接获取方法返回值
String strResult=result.getProperty("testReturn").toString();
// Log.i("--->result",""+strResult);
return strResult;
}
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
开机启动页面
package com.dongqin.cn.receiver;

import com.dongqin.cn.service.LocationService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class LocationReceiver extends BroadcastReceiver
{
//接受Intent的源
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(ACTION))   
{  
context.startService(
new Intent(context,LocationService.class));
//启动倒计时服务  
Toast.makeText(context, "广播服务启动!", Toast.LENGTH_LONG).show();
}
}
}

//manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.dongqin.cn"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="12" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<receiver android:name=".receiver.LocationReceiver">     
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />     
            </intent-filter>     
        </receiver>
        <activity android:name=".sendinfo.SendInfo" />
        <service android:name=".service.LocationService" android:enabled="true"/>
    </application>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

你可能感兴趣的:(webservice,axis,android客户端)