在启动8.1问题时重启服务

问题描述 投票:0回答:1

首先感谢您的回复。 :)

在过去的几个月里,我一直在研究一些针对android的后台记录应用程序。它们用于学术研究。自从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>

所以有人能看到我所缺少的东西吗?

android application-restart android-8.1-oreo
1个回答
0
投票

从奥利奥开始,不允许从后台启动服务,这与启动接收器的情况一样。幸运的是,你可以通过推迟使用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());
        }
    }

}

请查看this medium post以获取更详细的答案和代码。

© www.soinside.com 2019 - 2024. All rights reserved.