gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:53:22 25 4
gpt4 key购买 nike

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

这是我的 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>

最佳答案

在 list 中添加此权限,并在您的应用程序中添加此广播接收器类。我已经添加了我的服务 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);
}

}

关于java - android 设备重启时如何启动我的应用程序进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21088409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com