在引导 8.1 时重新启动服务问题



首先感谢您的回复。 :)

在过去的几个月里,我一直在为安卓开发许多后台日志记录应用程序。它们用于学术研究。自从android 8.1问世以来,我发现运行前台服务存在许多问题,但最大的问题是前台服务在启动时不会重新启动。这适用于前面的每个 SDK。

我已经构建了一个应用程序来解决这个问题。我只是想确认作业调度程序是否已从广播接收器激活。因此,数据存储在文件"data.txt"内部。

这是我在启动时重新启动前台服务的代码。

public class startServiceOnBoot extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent();
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
serviceIntent.setAction("geyer.sensorlab.jobschedulepractice.StartMyActivityAtBootReceiver");
Bundle b=new Bundle();
b.putBoolean("from main", true);
serviceIntent.putExtras(b);
Worker.enqueueWork(context, serviceIntent);
}
}

}

作业意向服务

public class Worker extends JobIntentService {
private static final String TAG = "Worker";
public static final int SHOW_RESULT = 123;
static final int DOWNLOAD_JOB_ID = 1000;
public static void enqueueWork(Context context, Intent intent) {
enqueueWork(context, Worker.class, DOWNLOAD_JOB_ID, intent);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
Log.d(TAG, "onHandleWork() called with: intent = [" + intent + "]");
if(Objects.requireNonNull(intent.getExtras()).getBoolean("from main")){
storeInternally("from main");
}else{
storeInternally("from restart");
}
}
private void storeInternally(String result) {
long timestamp = System.currentTimeMillis()/1000;
String dataEntry = result + " - " + timestamp + ": ";
try {
File path = this.getFilesDir();
File file = new File(path, "data.txt");
FileOutputStream fos = new FileOutputStream(file, true);
fos.write(dataEntry.getBytes());
fos.close();
Log.i("from screen service", dataEntry);
} catch (Exception e) {
Log.d("Main - issue writing", "Exception: " + e);
}
}

}

和清单:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".startServiceOnBoot"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<service android:name=".Worker"
android:permission="android.permission.BIND_JOB_SERVICE"/>
<service android:name=".target"/>
</application>

那么谁能看到我错过了什么?

我遇到了同样的问题。 当我给我的应用程序自动启动权限时,它起作用了。

下面给出的代码片段会将您路由到自动启动权限屏幕,您可以在其中手动授予该权限。

try
{
//Open the specific App Info page:
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
context.startActivity(intent);
}
catch ( ActivityNotFoundException e )
{
//Open the generic Apps page:
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
context.startActivity(intent);
}

由于奥利奥,不允许从后台启动服务,引导接收器就是这种情况。幸运的是,你可以通过使用JobIntent API推迟它来做到这一点,它会稍后启动,但它仍然保证迟早会启动。

public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
MyService.enqueueWork(context, new Intent());
}
}
}

看看这个中等帖子,以获得更详细的答案和代码。

最新更新