回想一下活动

时间:2017-02-23 12:03:11

标签: java android json

我想回忆一下12分钟之后的一项活动谁得到我​​的JSON但我不知道如何使用timertask和timershedule你能帮我吗? 我试过这个但是没有用 我的计划:

'order' => 'ASC/DESC'

我的日志

public class recuperationJson extends AppCompatActivity implements getData.ReturnValue {
private getData data;
private TextView t;
private String json_string;
private String json_url = "http://192.168.0.14/projet/php/fichier.php";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    data = new getData(json_url);
    data.setReturnListener(this);
    data.execute((Void) null);

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new Testing(), 0,5000);
}

@Override
public String renvoyerValeurString(String valeurARenvoyer) {
    data = null;
    //Ici je récupère directement mon json contenu dans la variable valeurARenvoyer
    json_string = valeurARenvoyer;
    Intent intent = new Intent(this,Accueil.class);
    intent.putExtra("json_data", json_string);
    startActivity(intent);
   // Toast.makeText(this, json_string, Toast.LENGTH_SHORT).show();
    return json_string;
}
public class Testing extends TimerTask {
    public void run() {
        Toast.makeText(recuperationJson.this, "test", Toast.LENGTH_SHORT).show();
    }
}

2 个答案:

答案 0 :(得分:1)

这对我有用。在调用课程安排你的计时器。

        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new Testing(), 0,5000);

在测试类中执行您的活动

      import java.util.TimerTask;


      public class Testing extends TimerTask
      {

     public void run()
     {
      //your task here
     }
     }

服务器关闭或重启后,定时器不起作用。最好使用石英。

答案 1 :(得分:0)

我想出了两种方法。

1 - 处理程序

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
     @Override
     public void run() {
        System.out.println("RUN AFTER 12 minutes");

     }
 }, 12*60*1000);

2 - AlarmManager

BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override public void onReceive( Context context, Intent _ )
            {
               System.out.println("RUN AFTER 12 minutes");
                context.unregisterReceiver( this ); 
            }
        };

        registerReceiver( receiver, new IntentFilter("message") );

        PendingIntent pintent = PendingIntent.getBroadcast( this, 0, new Intent("message"), 0 );
        AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));

        // set alarm to fire
        manager.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60*1000*12, pintent );