重启Android设备时如何启动我的应用程序进程?

时间:2014-01-13 10:02:14

标签: java android android-service

我正在制作连续跟踪用户位置的应用程序,到目前为止,我已经能够成功接收其位置更改的纵坐标,但是如果他重新启动手机而不是我无法再次启动我的服务打开应用程序。

这是我的Servicesstart.java代码

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



    }





      }

}

这是我的Broadcast.java代码

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

这是我的manifest.xml代码

<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>

1 个答案:

答案 0 :(得分:3)

在清单中添加此权限,并在您的应用程序中添加此广播接收器类。 我已添加服务 ServiceStarter 以首先启动,将其替换为您的

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

}