ソケットサーバーを作成してクライアントが接続するサービスがあります。接続はデータを転送するために開いたままです。サービスはSTART_STICKY
です。したがって、ユーザーが最近のタスクからスワイプしてアプリを殺すと、onCreate
とonStartCommand
関数が再び呼び出されます。私はソケットを再初期化したいのではなく、ユーザーがタスクをスワイプしてデータを強制終了し、データを途切れることなく転送し続ける前の状態を保持します。Android - サービスの再起動時にソケット接続を維持する方法
public class SendTasksService extends Service {
static Socket socket=null;
static boolean shouldRun=true;
static final int port = PORT_NO;
static ServerSocket listener = null;
@Override
public IBinder onBind(Intent intent) {
System.out.println("Service onBind.");
return null;
}
@Override
public void onCreate() {
System.out.println("Service onCreate.");
shouldRun=true;
try {
if(socket==null){
System.out.println("Starting Listening..");
listener = new ServerSocket(port);
System.out.println("Notification Started.");
}else{
System.out.println("Continuing With Old Socket..");
/*
How to get the socket if the connection
was made before swipe killing the app from recent
*/
}
}catch(Exception ioe){
System.out.println("Exception in listening.");
ioe.printStackTrace();
}
}
public void onDestroy() {
System.out.println("Service onDestroy.");
closeConnection();
}
public void closeConnection(){
//Wind up work and close connection
/*
This should also remove any socket information saved anywhere
*/
}
public int onStartCommand(Intent intent, int flags, int startid) {
System.out.println("Service onStartCommand.");
try{
new AsyncAction().execute();
}catch (Exception e) {
e.printStackTrace();
}
return START_STICKY;
}
private class AsyncAction extends AsyncTask<String, Void, String> {
protected String doInBackground(String... args) {
while(shouldRun) {
try {
if(socket==null) {
socket = listener.accept();
/*
Socket connection established here.
Save this socket state or preserve it somehow
So that connection does not break on killing the app
*/
}else{
/*
Continue with the old socket data, and resume connection
*/
}
if(socket==null)
continue;
}catch(Exception e){
System.out.println("Exception after Break");
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(String result) {
}
}
}
何を試してみましたか?
は
SharedPreferences
でgson
を使用してソケットを保存するようにしてください。 ソケットオブジェクトを戻すことができませんでした。SharedPreferences pref = getApplicationContext(). getSharedPreferences("MyPref", MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); editor.putBoolean("onGoingConnection",false); Gson gson = new Gson(); String json = gson.toJson(socket); editor.putString("ExistingSocket",json); editor.apply()
- サービスを開始したアクティビティに
static
セービングソケットを作成します。サービス再開時には常にnullだった。
私の接続が破壊されるのを防ぐことはできませんか?別の解決策は、データグラムを使用することができますが、それは私のクライアントも変更する必要があります。 – dumplingParadox
@dumplingParadox: "私の接続が破壊されないように保存できませんか?" - 正しい。あなたは、たとえサービスであっても、あなたのプロセスが永遠に続くと想定することはできません。プロセスが終了すると、システムが「START_STICKY」を使用していても、プロセスが再起動するまでに時間がかかることがあります。プロセスがなく、サーバーに接続していない任意の時間をサポートするようにアプリケーションを設計する必要があります。 – CommonsWare
私はその周りに道を見いだしました、あなたは状態を救うことができないということについて正しいです。正解と表示されます。 :)) – dumplingParadox