简体   繁体   中英

How to start my application process when a android device is rebooted?

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>

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);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM