0

IntentService を使用するときに結果を待つ方法はありますか?

シナリオ。

サービスアプリが実行されている->別のアプリからログイン資格情報を受け取る->サービスアプリがデータベースをチェックします(何らかの理由で常に接続タイムアウトが発生します。これが悪いことはわかっていますが、POCと簡単なハックの場合は今のところこれで十分です) --> 検証クエリを待ってから結果を取得します。

これは可能ですか?

サービス内で AsynTask を使用しようとしましたが、それでも役に立ちません。常に接続タイムアウト エラーが発生します。

デモサービス

@Override
public IBinder onBind(Intent intent) {

    return new IDemoService.Stub() {

        @Override
        /**
         * Login validation implementation
         */
        public boolean login(String id, String password)
                throws RemoteException {
            UserLoginTask userLoginTask = new UserLoginTask(id, password);
            try {
                return userLoginTask.execute((Void) null).get();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }
    };
}

ユーザーログインタスク

 private class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
    private String uId;
    private String uPassword;

    public UserLoginTask(String id, String password) {
        uId = id;
        uPassword = password;
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        Connection conn;
        try {
            int count = 0;
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://xxx.xxx.x.xxx/test_db?user=postgres&password=password";
            conn = DriverManager.getConnection(url);
            if (conn != null) {
                Statement st = conn.createStatement();
                String sql = "Select count(*) as cnt from tbl_persons where id = '"
                        + uId.toUpperCase(Locale.getDefault()).trim()
                        + "' AND pwd='" + uPassword.trim() + "';";

                Log.d("DemoService", "Login sql - " + sql);
                ResultSet rs = st.executeQuery(sql);
                while (rs.next()) {
                    count = rs.getInt("cnt");
                }

                rs.close();
                st.close();
                conn.close();

                if (count == 0)
                    return false;

                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

}
4

0 に答える 0