如果重新启动设备之前服务正在运行,我想在重新启动设备后启动我的服务。我处理了启动完成的意图,但是当我想再次启动服务时,我的应用程序终止了。
我的BroadcastReceiver:
public class BootCompletedIntentReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent serviceIntent = new Intent(context.getApplicationContext(), LocationService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
context.getApplicationContext().startForegroundService(serviceIntent);
}
else
{
context.getApplicationContext().startService(serviceIntent);
}
}
}
LocationService的一部分:
@Override
public void onCreate()
{
...
startForeground(NotificationCreator.getNotificationId(), NotificationCreator.getNotification(this));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
...
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
}
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Toasty.warning(this, "Turn on GPS", Toast.LENGTH_SHORT).show();
isPause = true;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);
return Service.START_NOT_STICKY;
}
我开始服务的课程的一部分:
Intent serviceIntent = new Intent(getContext(), LocationService.class);
serviceIntent.putExtra(IntentExtras.COURSE, Parcels.wrap(course));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
getActivity().startForegroundService(serviceIntent);
}
else
{
getActivity().startService(serviceIntent);
}
清单:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-feature android:name="android.hardware.location1.gps" />
<application>
<service android:name=".services.LocationService" />
<receiver android:name=".BootCompletedIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
我启动应用程序并启动服务,当服务正常运行时,我会重新启动设备。重新启动后,在BroadcastReceiver中调用onReceive方法。我可以使用此方法在第一行中处理断点,但是调用启动服务后代码将停止执行。我试图在以这种方法启动它们之前停止服务,但是id并没有改变任何东西。我还尝试了在没有广播接收器的情况下重新启动后启动应用程序后启动服务,但这也无济于事。
答案 0 :(得分:0)
将此添加到您的清单中
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="BootCompletedIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
哦,我想我知道为什么
您不能在Service
内运行此代码。它必须在Activity
中运行。
if (
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
){
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Toasty.warning(this, "Turn on GPS", Toast.LENGTH_SHORT).show();
isPause = true;
}
}
答案 2 :(得分:0)
首先,您需要在AndroidManifest.xml中获得许可:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
此外,在yourAndroidManifest.xml中,定义您的服务并监听BOOT_COMPLETED操作:
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartMyServiceAtBootReceiver"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
然后,您需要定义将执行BOOT_COMPLETED操作并启动服务的接收器。
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent);
}
}
}
现在,当手机启动时,您的服务应已运行。