Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call service methods from broadcastReceiver?
    primarykey
    data
    text
    <p>I have GPS Tracking application main goal is saving GPS coordinate to backed database every 5 minutes interval. So i created <code>Service</code> &amp; <code>receiver</code> because even my my application doesn't open / run this should work.</p> <p>After user enter executive code , it create database and go to welcome screen. In there it start GPS capturing &amp; save it to PDA database calling service to upload. I have receiver, when phone is<code>Booted</code> it start this receiver &amp; receiver call service.</p> <p>My problem is receiver doesn't call service. It didn't go to Service class.</p> <pre><code> 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(); } } </code></pre> <p>This is my receiver .</p> <pre><code> 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); } } </code></pre> <p>}</p> <p>This is my service.</p> <pre><code> 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&lt;?&gt; stringList = gpsAdapter.selectRecordsFromDBList(query, null); System.out.println("==WMLiveGPSData==stringList=="+stringList.size()); gpsAdapter.close(); if(stringList.size() &gt; 0){ for (int i = 0; i &lt; stringList.size(); i++) { ArrayList&lt;?&gt; arrayList = (ArrayList&lt;?&gt;) stringList.get(i); ArrayList&lt;?&gt; list = arrayList; HttpResponse response = null; ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); 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(); } }; </code></pre> <p>}</p> <p>This is my manifest file</p> <pre><code> &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" &gt; &lt;activity android:name=".MassGPSTrackingActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".HomeActivity" android:screenOrientation="unspecified"&gt;&lt;/activity&gt; &lt;service android:enabled="true" android:name=".service.UploadService" /&gt; &lt;receiver android:name="com.mass.gps.service.GpsReceiver"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt; &lt;action android:name="android.intent.action.BATTERY_CHANGED" /&gt; &lt;action android:name="android.intent.action.ACTION_POWER_CONNECTED"/&gt; &lt;action android:name="android.intent.action.SCREEN_ON"/&gt; &lt;action android:name="android.intent.action." /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;/application&gt; &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_GPS"&gt;&lt;/uses-permission&gt; &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt; &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.WAKE_LOCK"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; </code></pre> <p></p> <p>Issue is it didn't go to Service class.</p> <p>Please help me out this question...</p> <p>Thanks in advance..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload