杀死应用程序

时间:2017-02-11 13:22:34

标签: java android android-service

我已在START_STICKY模式

中创建了服务

以下是我的服务类

public class KeepAliveStatusMessageService extends Service {
private Timer keepAliveStatusMessageTimer;
private TimerTask timerJob;
private JsHandler javaSHandler;

protected void doInBackground() {
    javaSHandler =LoginActivity.javaSHandler; //JSHandlerSingleton.getInstance().getJavaSHandlerLogin();
    keepAliveStatusMessageTimer = new Timer();
    AllTimer.getInstance().setKeepAliveStatusMessageTimer(keepAliveStatusMessageTimer);
    timerJob = new TimerTask() {
        @Override
        public void run() {
            UserInformationProvider userInformationProvider = UserInformationProvider.getInstance();
            javaSHandler =GladiusLoginActivity.javaSHandler;
            Log.v("Service","called");
            javaSHandler.keepAliveStatusMessage(userInformationProvider.getIpAddress(), userInformationProvider.getUsername(), userInformationProvider.getSessionId());
        }

        ;
    };
    keepAliveStatusMessageTimer.schedule(timerJob, 5 * 1000, 5 * 1000);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("LocalService", "Received start id " + startId + ": " + intent);
    doInBackground();
    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

当我使用我的应用程序或进入后台时,此服务可以正常工作, 但是当我通过从背景中滑动来杀死我的应用程序时,我收到了以下错误

  

致命异常:Timer-0                                                                             过程:com.me.cst,PID:18897                                                                             java.lang.NullPointerException:尝试调用虚方法' void com.me.interfaces.JsHandler.keepAliveStatusMessage(java.lang.String,java.lang.String,java.lang.String)'在null对象引用上                                                                                 在com.me.service.KeepAliveStatusMessageService $ 1.run(KeepAliveStatusMessageService.java:67)                                                                                 在java.util.Timer $ TimerImpl.run(Timer.java:284)

我不知道为什么JsHandler在杀死应用程序后变为null。 杀死应用程序后请帮助我调用该方法,因为只有当该值变为空时服务才会中止。 任何建议表示赞赏.. 请原谅我的英文......

EDIT1: JsHandler是JsHandler.java类的遗骸。这个类用于调用java脚本方法,从那里发送网络调用。 以下是JsHandler类的简介 的 JsHandler.java

    public class JsHandler {
    Activity activity;
    WebView webView;
    Handler handler = new Handler();
   public JsHandler(Activity _contxt, WebView _webView, Handler handler) {
        activity = _contxt;
        webView = _webView;
        this.handler = handler;
    }
public void keepAliveStatusMessage(String ip, String username, String sessionId) {
        final String webUrl = "javascript:keepAliveStatusMessage('" + ip + "','" + username + "','" + sessionId + "')";
        activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                webView.loadUrl(webUrl);
            }
        });
    }
}

我在LoginActivity.java中创建了一个JsHandler的静态变量 我把它变成了静态变量,因为我无法通过putExtra方法传递对象

public  static JsHandler javaSHandler;

并在启动方法中指定值

javaSHandler = new JsHandler(this, loginWebView, new Handler());

在KeepAliveStatusMessage.java类中,我正在访问该静态变量。 调用JsHandler类的keepAlive方法。

1 个答案:

答案 0 :(得分:0)

I don't know why JsHandler becoming null after killing the app.

Your process was terminated. Then, Android started up a fresh process for you, to start a fresh copy of your sticky service. However, you are referring to a static field (GladiusLoginActivity.javaSHandler), which will not be initialized in this new process. Hence, you crash when trying to call methods on that object, since it will be null.