2

I am making app which tracks user location continuously, so far I have been able to make a successful receiving of its ordinate on location change, but if he is restarting the phone than I am not able to start my service without user again opening the app.

This is my Servicesstart.java code

    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    Log.i("service started", "start");

      userFunctions = new UserFunctions();
      final LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      if(userFunctions.isUserLoggedIn(getApplicationContext())){

        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        imei=telephonyManager.getDeviceId();
          Toast.makeText(getApplicationContext(), imei, 5000).show();


    if(Broadcast.check==false)
    {



         final UserFunctions userFunction = new UserFunctions();

        LocationListener locatioListner = new LocationListener() {


            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
                get();
            }

            public void onLocationChanged(Location location) {

                Log.i("location", "loc");
                String latitude=String.valueOf(location.getLatitude());
                String longtitude=String.valueOf(location.getLongitude());
                //location updated starts here
                boolean check_update=userFunction.locationUpdater(UserFunctions.email, latitude, longtitude,imei);

                if(check_update==true){

                Log.i("updated", "update");
                        }
                else
                {
                 Log.i("notupdated","not");
                }

                }

        };

        locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locatioListner);



    }





      }

}

this is my Broadcast.java code

public class Broadcast extends BroadcastReceiver {
 public static boolean check=true;

 @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
    // TODO Auto-generated method stub
     if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
         check=false;
            Intent i = new Intent(context,Servicestart.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(i);
        }
}
}

This my manifest.xml code

<service android:name=".Servicestart"> 
            </service> 
            <receiver android:name=".Broadcast"
                android:enabled="true"
                android:exported="true"> 
                <intent-filter android:priority="1000"> 
                    <action android:name="android.intent.action.BOOT_COMPLETED" /> 
                    </intent-filter> 
                    </receiver>
Kitkat
  • 77
  • 16
rohit
  • 107
  • 2
  • 7
  • 2
    how do YOU feel when an application starts without your explicit consent in your mobile or PC? – STT LCU Jan 13 '14 at 10:03
  • Try to put a breakpoint or log in onReceive method and check if you can capture the event. Probably your receiver is working but you have a context problem for your intent. – Barışcan Kayaoğlu Jan 13 '14 at 10:12
  • 1
    Oh and did you add the permission for it? – Barışcan Kayaoğlu Jan 13 '14 at 10:14
  • hey Barışcan Kayaoğlu,thanks for your reply, yes i add the permission but stil this is not working pls help me.. – rohit Jan 13 '14 at 10:19
  • @BarışcanKayaoğlu ,how to solve a context problem in my intent – rohit Jan 13 '14 at 10:21
  • Well you can try context = getApplicationContext(); to refresh your active context. I'm not sure if it's the best work around but it did solve my problem before. But in my example i was using a service, not receiver and Receiver class does not have the method getApplicationContext. I'm not sure if you can start up an intent from a receiver. – Barışcan Kayaoğlu Jan 13 '14 at 11:57
  • possible duplicate of [Trying to start a service on boot on Android](http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android) – molnarm Jan 13 '14 at 12:22

1 Answers1

3

Add this permission in manifest and add this broadcast receiver class in your application. I have added my service ServiceStarter to start first, replace this with yours

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

public class StartupReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent arg1) {
        // TODO Auto-generated method stub

         System.out.println("Reciver Started..........................................");

        Intent activate = new Intent();
        activate.setClass(context, **ServiceStarter.class**);
//        activate.putExtra("Auto", true);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activate,
                Intent.FLAG_ACTIVITY_NEW_TASK);
        AlarmManager mgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 20000, pendingIntent);
    }

}
Athul
  • 231
  • 1
  • 7