通过计时器调用方法

时间:2018-01-06 09:15:57

标签: java android timer textview

我有一个工作方法,它正在改变TextView中的文本。 我已经通过计时器运行另一种方法来复制它 - 它不起作用。 请解释一下我的问题在哪里?

public void timer(final int timel){                             
        ScheduledExecutorService service2 = Executors.newSingleThreadScheduledExecutor();
        service2.schedule(new Runnable() {
            @Override
            public void run() {
                alert();
            }
        }, timel, TimeUnit.SECONDS);

    }

P.S。当然方法alert()正在工作(我可以在任何地方调用它,但不是在那里)。

提醒功能: 我如何使用"静态"对于我已发表评论的行" HERE"?

public void alert(){

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);//HERE
if (tmp=="") //loading from FireBase
{
    chargeText = ":)";
}
        builder.setTitle(R.string.text)
                .setMessage(chargeText)
                .setIcon(R.drawable.gatsby)
                .setCancelable(false)
                .setPositiveButton(R.string.share,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                final Intent intent = new Intent(Intent.ACTION_SEND);
                                intent.setType("text/plain");
                                String textToSend= "Some text" + chargeText.toLowerCase() + ". Some text?";
                                intent.putExtra(Intent.EXTRA_TEXT, textToSend);
                                try
                                {
                                    startActivity(Intent.createChooser(intent, "Description of method"));//HERE
                                }
                                catch (android.content.ActivityNotFoundException ex)
                                {
                                    Toast.makeText(getApplicationContext(), "Some error", Toast.LENGTH_SHORT).show();//HERE
                                }
                            }
                        });
        builder.setNegativeButton(R.string.exit,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

1 个答案:

答案 0 :(得分:1)

您需要致电YOURCLASS.alert();,因为您现在位于Runnable内。