2

GPS追跡アプリケーションの主な目標は、5分間隔ごとにGPS座標をバックアップされたデータベースに保存することです。私のアプリケーションでさえも開いたり実行したりしないので、これは動作するはずですServicereceiver

ユーザーが実行コードを入力すると、データベースが作成され、ようこそ画面に移動します。そこで GPS キャプチャを開始し、それを PDA データベース呼び出しサービスに保存してアップロードします。私は受信機を持っています。電話の場合、Bootedこの受信機と受信機の通話サービスを開始します。

私の問題は、受信者がサービスを呼び出さないことです。サービスクラスには行きませんでした。

  protected void onStop(){
    super.onStop();
    if(gpsReceiver != null){
        unregisterReceiver(gpsReceiver);
    }
}

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    gpsReceiver = new GpsReceiver();
    IntentFilter intentFilter1 = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
    intentFilter1.addAction(Intent.ACTION_BOOT_COMPLETED);
    intentFilter1.addAction(Intent.ACTION_POWER_CONNECTED);
    intentFilter1.addAction(Intent.ACTION_SCREEN_ON);
    registerReceiver(gpsReceiver, intentFilter1);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MINIMUM_TIME_BETWEEN_UPDATES, 
           MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
           new MyLocationListener()
    );
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new MyLocationListener());
 }

 private class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {
            String message = String.format( "Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude());
            longitude = location.getLongitude();
            latitude =location.getLatitude();

            //save GPS coordinate to PDA DB
            GPSDBAdapter dbAdapter = GPSDBAdapter.getDBAdapterInstance(HomeActivity.this);
            dbAdapter.openDataBase();
            dbAdapter.insertGPS(longitude, latitude, "MASS", deserializeObject());
            dbAdapter.close(); 

            //After save GPS coordinate it upload to backend using service
            startService(new Intent(HomeActivity.this, UploadService.class));
            Toast.makeText(HomeActivity.this, message, Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(HomeActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
           Toast.makeText(HomeActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
        }
        public void onProviderEnabled(String s) {
          Toast.makeText(HomeActivity.this, "Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
        }

    }

これは私の受信機です。

    public class GpsReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
    int delay = 5000; // delay for 5 sec.
    //int period = 1000 *60*5; // repeat every 5min.
    int period = 30000; // repeat every 5min.

    //TO-REMOVE -TESTING PURPOSE
     Intent serviceIntent = new Intent(context,UploadService.class);
     context.startService(serviceIntent);

     if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            System.out.println(" Receiver done");
            Intent serviceIntent = new Intent(context,UploadService.class);
            context.startService(serviceIntent);
        }

        }, delay, period);
    }
}

}

これが私のサービスです。

   public class UploadService extends Service{
private Thread serviceThread = null;
public static final String APPURL = "http://124.43.25.10:8080/Ornox/GPSPulseReceiver";

public static double longitude;
public static  double latitude ;

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

@Override
public void onCreate() {
    Log.d("========", "onCreate");
    Toast.makeText(UploadService.this, "Upload GPS Service Created", Toast.LENGTH_LONG).show();

}

@Override
public void onDestroy() {
    Toast.makeText(UploadService.this, "Upload Service Stopped", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(UploadService.this, "Upload Service Started", Toast.LENGTH_LONG).show();

    ConnectivityManager manager = (ConnectivityManager) getSystemService(MassGPSTrackingActivity.CONNECTIVITY_SERVICE);
    boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
    boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
    if(is3g ||isWifi){
        if(!APPURL.equals("")){
            serviceThread = new ServiceThread();
            serviceThread.start();

        }
    }else {
        Toast.makeText(this, "GPRS/WIFI is not available", Toast.LENGTH_LONG).show();
    }

}


public void uploadGPSData() {
    GPSDBAdapter gpsAdapter = GPSDBAdapter.getDBAdapterInstance(this);
    gpsAdapter.openDataBase();
    try{
        String query = " SELECT ExecutiveCode,CaptureDate,CaptureTime,Longitude,Latitude" +//4
                        " FROM WMLiveGPSData " +
                        " WHERE UploadFlag ='1' ";
         ArrayList<?> stringList = gpsAdapter.selectRecordsFromDBList(query, null);
         System.out.println("==WMLiveGPSData==stringList=="+stringList.size());
         gpsAdapter.close();
         if(stringList.size() > 0){
             for (int i = 0; i < stringList.size(); i++) {
                 ArrayList<?> arrayList = (ArrayList<?>) stringList.get(i);
                 ArrayList<?> list = arrayList;
                 HttpResponse response = null;

                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("repc", (String)list.get(0)));
                    nameValuePairs.add(new BasicNameValuePair("rouc", "SE0160"));
                    nameValuePairs.add(new BasicNameValuePair("Date", (String)list.get(1)));
                    nameValuePairs.add(new BasicNameValuePair("Time", (String)list.get(2)));
                    nameValuePairs.add(new BasicNameValuePair("long", (String)list.get(3)));
                    nameValuePairs.add(new BasicNameValuePair("lat", (String)list.get(4)));
                    try {
                        HttpParams httpParameters = new BasicHttpParams();
                        int timeoutConnection = 3000000;
                        HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
                        int timeoutSocket = 5000000; // in milliseconds which is the timeout                                
                        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
                        HttpClient httpclient = new DefaultHttpClient(httpParameters);
                        HttpPost method = new HttpPost(APPURL);
                        // method.setHeader("Content-Type","text/xml");

                        method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        response = httpclient.execute(method);
                        System.out.println("==response==" + response);
                        if (response != null) {
                            Log.i("login",""+ response.getEntity().getContentLength());
                        } else {
                            Log.i("login", "got a null response");
                        }

                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(),
                                "Could not connect to server. Please Try again",
                                Toast.LENGTH_SHORT).show();
                        Log.e("log_tag", "Error in http connection " + e.toString());
                    }
             }

         }
    }catch (Exception e) {
        e.printStackTrace();
    }

    //return response;
}

private class ServiceThread extends Thread {
    @Override
    public void run() {
          uploadGPSData();
    }   
};

}

これは私のマニフェストファイルです

     <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MassGPSTrackingActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

     <activity android:name=".HomeActivity"   android:screenOrientation="unspecified"></activity>
     <service android:enabled="true" android:name=".service.UploadService" />

    <receiver android:name="com.mass.gps.service.GpsReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.BATTERY_CHANGED" />
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
             <action android:name="android.intent.action.SCREEN_ON"/>
            <action android:name="android.intent.action." />
        </intent-filter>
    </receiver>

</application>

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

問題は、サービス クラスに移動しなかったことです。

この質問を助けてください...

前もって感謝します..

4

2 に答える 2

1

パッケージ名が正しいマニフェスト ファイルではないと思います

  <service android:enabled="true" android:name="com.mass.gps.service.UploadService" />

サービス名をパッケージ名として指定します

于 2012-04-24T08:56:08.203 に答える
1

これをレシーバーで使用します。

Intent i = new Intent();
i.setClassName("com.intelligent.locator", "com.intelligent.locator.AlarmActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
于 2012-08-07T09:24:58.243 に答える