如何在更改数据值后重新启动Handler?

时间:2017-07-18 08:26:37

标签: java android handler

早上好StackOverFlow 我在Handler遇到了一些问题我想尝试MainActivity我的 Client.java < / strong> 50秒后也会发送消息并停止客户端和该工作,但我必须在设置中ip_txtterm_txt的每次更改时重新打开连接。的java  (通过单击按钮将数据从EditText保存到DB)

这是我的 MainActivity

        public class MainActivity extends AppCompatActivity {
    Server server;
    Client client;
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
    @Override
    public void run() {
        new ConnectTask().execute("");
        if (client != null) {
            client.sendMessage("IP#" + ipp + " " + "N#" + trm);
        }
        if (client != null) {
            client.stopClient();
        }
    }
};
settings Settings;
public static TextView terminale, indr, msg;
TextView log;
String ipp,trm;
DataBaseHandler myDB;
allert Allert;
SharedPreferences prefs;
String s1 = "GAB Tamagnini SRL © 2017 \n" +
        "Via Beniamino Disraeli, 17,\n" +
        "42124 Reggio Emilia \n" +
        "Telefono: 0522 / 38 32 22 \n" +
        "Fax: 0522 / 38 32 72 \n" +
        "Partita IVA, Codice Fiscale \n" +
        "Reg. Impr. di RE 00168780351 \n" +
        "Cap. soc. € 50.000,00 i.v. \n" + "" +
        "REA n. RE-107440 \n" +
        "presso C.C.I.A.A. di Reggio Emilia";
ImageButton settings, helps, allerts, home;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Utils.darkenStatusBar(this, R.color.colorAccent);
    server = new Server(this);
    myDB = DataBaseHandler.getInstance(this);

    msg = (TextView) findViewById(R.id.msg);
    log = (TextView) findViewById(R.id.log_avviso);
    settings = (ImageButton) findViewById(R.id.impo);
    helps = (ImageButton) findViewById(R.id.aiut);
    allerts = (ImageButton) findViewById(R.id.msge);
    home = (ImageButton) findViewById(R.id.gab);
    terminale = (TextView) findViewById(R.id.terminal);
    indr = (TextView) findViewById(R.id.indr);

    final Cursor cursor = myDB.fetchData();
    if (cursor.moveToFirst()) {
        do {
            indr.setText(cursor.getString(1));
            terminale.setText(cursor.getString(2));
            Client.SERVER_IP = cursor.getString(1);
            trm = cursor.getString(2);
        } while (cursor.moveToNext());
    }



    WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    ipp = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());


    handler.postDelayed(runnable,5000);

    cursor.close();
    server.Parti();


    home.setOnClickListener(new View.OnClickListener() {
                                int counter = 0;
        @Override
        public void onClick(View view) {
                                    counter++;
                                    if (counter == 10) {
                                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                        builder.setCancelable(true);
                                        builder.setMessage(s1);
                                        builder.show();
                                        counter = 0;
                                    }
        }
    });

    settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent impostazioni = new Intent(getApplicationContext(), settingsLogin.class);
            startActivity(impostazioni);
        }
    });

    helps.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent pgHelp = new Intent(getApplicationContext(), help.class);
            startActivity(pgHelp);
        }
    });

    allerts.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Server.count = 0;
            SharedPreferences prefs = getSharedPreferences("MY_DATA", MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.clear();
            editor.apply();
            msg.setVisibility(View.INVISIBLE);
            Intent pgAlert = new Intent(getApplicationContext(), allert.class);
            startActivity(pgAlert);
        }
    });

}

@Override
protected void onDestroy() {

    super.onDestroy();
    server.onDestroy();
}

public class ConnectTask extends AsyncTask<String, String, Client> {

    @Override
    protected Client doInBackground(String... message) {


        client = new Client(new Client.OnMessageReceived() {
            @Override

            public void messageReceived(String message) {

                publishProgress(message);
            }
        });
        client.run();

        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);

        Log.d("test", "response " + values[0]);


    }
}

}

这是我的 settings.java

public class settings extends AppCompatActivity {

TextView indr;
Client client;
EditText ip_txt,term_txt;
ImageButton home;
Button save;
DataBaseHandler myDB;
MainActivity activity;
String ipp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myDB = DataBaseHandler.getInstance(this);
    setContentView(R.layout.activity_settings);
    Utils.darkenStatusBar(this, R.color.colorAccent);

    home = (ImageButton) findViewById(R.id.stgbtn);
    indr = (TextView) findViewById(R.id.ipp);
    ip_txt = (EditText) findViewById(R.id.ip);
    term_txt = (EditText) findViewById(R.id.nTermin);
    save = (Button) findViewById(R.id.save);


    Cursor cursor = myDB.fetchData();
    if(cursor.moveToFirst()){
        do {
            ip_txt.setText(cursor.getString(1));
            term_txt.setText(cursor.getString(2));
        }while(cursor.moveToNext());
    }
    cursor.close();
    AddData();

    home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });




    WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    ipp = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
    indr.setText(ipp);


}

    public void AddData() {
        save.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myDB.insertData(ip_txt.getText().toString(),
                                term_txt.getText().toString());
                        MainActivity.indr.setText(ip_txt.getText().toString());
                        MainActivity.terminale.setText(term_txt.getText().toString());
                        Client.SERVER_IP = ip_txt.getText().toString();
                        finish();
                    }
                }
        );
    }

    }

如果我没有解释,我想在应用程序启动时启动 handler 在我更改 EditText ip_txt {{1}中的数据后立即重新启动或立即启动它 })

1 个答案:

答案 0 :(得分:1)

我会创建变量来存储处理程序和runnable,然后当你想重新启动它时只需使用:

yourHandler.removeCallback(yourRunnable);
yourHandler.postDelayed(yourRunnable,time);

并启动它,就像你那样做,

yourHandler.postDelayed(yourRunnable,5000);

希望就是你要找的。

如何创建它们:

public class ClassName{
    Handler yourHandler = new Handler();
    Runnable yourRunnable = new Runnable(){
        @Override
        public void run() {
            //yourCode
        }
    };
    // Rest of class code

}

与您在代码中的操作相同,但是在变量中。