0

Android プロジェクトでMikrotik Java APIを使用しています

私が達成しようとしているのは:

  • 情報でログイン
  • バックグラウンドで接続を保存する
  • すべてのアクティビティで接続を使用し、さまざまなアクティビティで接続を共有して、他のアクティビティApiConnectionと同じように使用しますcon.login() con.execute()

ログインフォームを送信した後、情報を送信し、intentServiceを開始し、そこで接続を確立します

@Override
protected void onHandleIntent(Intent intent) {
    try {
        try {
            String host = intent.getStringExtra("host");
            String username = intent.getStringExtra("username");
            String password = intent.getStringExtra("password");

            ApiConnection con = ApiConnection.connect(host);
            con.login(username, password);

            // Connected

        } catch (Exception e) {
            // Exception
        }
    } catch (Exception e) {
        // Exception
    }
}

そのインテントサービスからの接続に成功しましたが、接続に問題がある場合は例外を処理できません。トーストメッセージもonHandleIntentに表示されません。

私がしていることは意味がなく、論理的ではないと思います。

どうすればそのようなことを達成できますか?

4

2 に答える 2

0

接続を取得するメインアクティビティでconオブジェクトを静的として宣言し、要件に従ってどこでも使用してみてください。

以下の構造を参照してください。

主な活動中

   public static ApiConnection con;

接続について

    con = ApiConnection.connect(host);

以下のように、メイン アクティビティでこの接続を取得するためのメソッドを 1 つ作成します。

   public static ApiConnection getCon()
    {
        if(con != null)
           return con;
         else
           return null;
    }

以下のコードにより、任意のアクティビティでこのオブジェクトを使用します

   ApiConnection con = = MainActivity.getCon();
   if(con != null)
    {
       // Use Your Logic Here
    }
于 2016-06-18T04:24:02.933 に答える
0

@Amr SubZero私はAPIのセットアップを持っていないので、フローを説明するためにこのクラスを作成しました。

import android.util.Log;

/**
 * Created by Wasim on 18-06-2016.
 */
public class SingletonApiConnection {

    private static final String TAG = "SingletonApiConnection";

    private static SingletonApiConnection apiInstance = null;

    private SingletonApiConnection() {
    }

    public SingletonApiConnection(String host) {
        apiInstance.connect(host);
        Log.d(TAG, "Object Created.");
    }

    public static SingletonApiConnection getInstance(String host) {

        if (null == apiInstance) {
            apiInstance = new SingletonApiConnection(host);
        }

        return apiInstance;
    }
}

インテントサービス

public class NetworkRequestService extends IntentService {

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public NetworkRequestService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        // get object with host
        String host = intent.getStringExtra("host");
        SingletonApiConnection connection = SingletonApiConnection.getInstance(host);

        String username = intent.getStringExtra("username");
        String password = intent.getStringExtra("password");

        // now call login
        connection.login(username, password);

        // now use this at any class it will have loggedin instance b'z it is singleton.

    }
}

そしてログインで

Intent networkService = new Intent(LoginActivity.this, NetworkRequestService.class);
// add intent parameters here
startService(networkService);
于 2016-06-18T05:54:36.130 に答える