小编Ani*_*Dey的帖子

从后台删除应用程序时如何停止JobService Scheduled?

目前,我正在使用一个应用程序,并且我的应用程序具有用户可以单击“导航”按钮并且我的应用程序将启动Google Map的功能。到现在为止一切都很好,我已经做到了。但是,我被困的事实是我希望我的应用执行某些任务。为此,我使用了JobService并将其安排为每5秒运行一次,即使该应用程序处于后台也是如此。

当用户按下后退按钮时,则在onDestroy方法内部,我已取消了调度程序。但是,当通过滑动或按下十字图标将应用程序从后台删除时,JobService会继续运行,因为从后台删除应用程序时,os可以调用onDestroy方法。从后台删除应用程序后,如何停止计划的工作?

在此处输入图片说明

在此处输入图片说明

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="javarank.com.serviceinbackground">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MyJobService" android:exported="true" android:permission="android.permission.BIND_JOB_SERVICE" />

    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

MyJobService类别

public class MyJobService extends JobService {

    @Override
    public boolean onStartJob(final JobParameters jobParameters) {
        Toast.makeText(getApplicationContext(), "Doing job", Toast.LENGTH_SHORT).show();
        jobFinished(jobParameters, true);
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的MainActivity

public class MainActivity extends AppCompatActivity …
Run Code Online (Sandbox Code Playgroud)

java android google-maps jobservice

5
推荐指数
1
解决办法
3271
查看次数

在iOS中解析Json响应时出现valueNotFound错误

我正在尝试使用解析响应JSONDecoder。如果相应的键有值,那么它将很好,但是如果键的值为空,则它将无法编译,并出现以下错误。

valueNotFound(Swift.String,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:“ Results”,intValue:nil),_JSONKey(stringValue:“ Index 0”,intValue:0),CodingKeys(stringValue:“ VehicleName”, intValue:nil)],debugDescription:“期望的字符串值,但发现为null。”,底层错误:nil))

guard let obj = try? JSONDecoder().decode(ShipmentsResponse.self, from: json) else {return}
Run Code Online (Sandbox Code Playgroud)

这是我违抗的货运类

class ShipmentResponse : Codable {

    var ItemId: String = ""
    var VehicleName: String  = ""
    var VehicleNumber: String  = ""

    convenience required init(from decoder: Decoder) throws
    {
        self.init()
        let values = try decoder.container(keyedBy: CodingKeys.self)
        ItemId = try values.decode(String.self, forKey: .ItemId)

        do {
            _ = try values.decode(String.self, forKey: .VehicleName)
        } catch {
            print(error)
        }

        VehicleName = try values.decode(String.self, forKey: .VehicleName)
        VehicleNumber …
Run Code Online (Sandbox Code Playgroud)

parsing json ios swift jsondecoder

5
推荐指数
1
解决办法
433
查看次数

标签 统计

android ×1

google-maps ×1

ios ×1

java ×1

jobservice ×1

json ×1

jsondecoder ×1

parsing ×1

swift ×1