Android 如何使用可选择的时间多次执行asynctask?
我在MainActivity上有7个单选按钮,每个按钮需要不同的时间(1分钟,5分钟,10分钟,…),我在那里有asynctask,我正在从服务器(php)调用图片,所以我希望当用户选择第一个单选按钮(1分钟)时,asynctask每1分钟执行一次,我尝试radiobutton.ischecked and Putte handler它没有成功 我的服务器正在工作,我正在收到响应,但我无法使用此设置来执行asynctask以设置墙纸。它查看了我的代码Android 如何使用可选择的时间多次执行asynctask?,android,android-asynctask,handler,Android,Android Asynctask,Handler,我在MainActivity上有7个单选按钮,每个按钮需要不同的时间(1分钟,5分钟,10分钟,…),我在那里有asynctask,我正在从服务器(php)调用图片,所以我希望当用户选择第一个单选按钮(1分钟)时,asynctask每1分钟执行一次,我尝试radiobutton.ischecked and Putte handler它没有成功 我的服务器正在工作,我正在收到响应,但我无法使用此设置来执行asynctask以设置墙纸。它查看了我的代码 TextView txt; Button
TextView txt;
Button btn;
ImageView imageView;
String forecastJsonStr;
RadioButton rd1,rd2,rd3,rd4,rd5,rd6,rd7;
Runnable mHandlerTask;
Handler mHandler;
RadioGroup radioGroup;
private final static int INTERVAL = 4000 ; //1 min
private final static int INTERVAL2 = 1000 * 60 * 5; // 5 min
private final static int INTERVAL3 = 1000 * 60 * 10; // 10 min
private final static int INTERVAL4 = 1000 * 60 * 15; // 15 min
private final static int INTERVAL5 = 1000 * 60 * 30 ; // 30 min
private final static int INTERVAL6 = 1000 * 60 * 60; // 1 hour
private final static int INTERVAL7 = 1000 * 60 * 1440; // 1 day
private final String hostName = "http://555.555.555.555";
private final String hostRequestName = "/yay.php";
private final String hostWallpaperName = "/wallpaper/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.textView);
imageView = (ImageView) findViewById(R.id.imageView);
rd1 = (RadioButton)findViewById(R.id.radioButton) ;
rd2 = (RadioButton)findViewById(R.id.radioButton2) ;
rd3 = (RadioButton)findViewById(R.id.radioButton3) ;
rd4 = (RadioButton)findViewById(R.id.radioButton4) ;
rd5 = (RadioButton)findViewById(R.id.radioButton5) ;
rd6 = (RadioButton)findViewById(R.id.radioButton6) ;
rd7 = (RadioButton)findViewById(R.id.radioButton7) ;
radioGroup = (RadioGroup)findViewById(R.id.radiogroup) ;
mHandler = new Handler();
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(rd1.isChecked()) {
mHandlerTask = new Runnable() {
@Override
public void run() {
new WallpaperData().execute();
mHandler.postDelayed(mHandlerTask, INTERVAL);
startRepeatingTask();
}
};
}
}
void startRepeatingTask()
{
mHandlerTask.run();
}
});
}
private class WallpaperData extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL("http://555.555.555.555/yay.php");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
DataOutputStream wr = new DataOutputStream(
urlConnection.getOutputStream());
wr.write("method=get_random_wallpaper".getBytes());
wr.flush();
wr.close();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
Log.d("hey", buffer.toString());
}
if (buffer.length() == 0) {
return null;
}
forecastJsonStr = buffer.toString();
return forecastJsonStr;
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
}
@Override
protected void onPostExecute(final String forecastJsonStr) {
txt.setText(forecastJsonStr);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
Bitmap result = Picasso.with(getBaseContext())
.load(hostName + hostWallpaperName + forecastJsonStr)
.get();
wallpaperManager.setBitmap(result);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
thread.start();
super.onPostExecute(forecastJsonStr);
}
}
}
TextView-txt;
按钮btn;
图像视图图像视图;
字符串预测JSONSTR;
单选按钮rd1、rd2、rd3、rd4、rd5、rd6、rd7;
可运行的mhandler任务;
汉德勒;
放射组放射组;
专用最终静态整数间隔=4000//1分钟
专用最终静态整数间隔2=1000*60*5;//5分钟
专用最终静态整数间隔3=1000*60*10;//10分钟
专用最终静态整数间隔4=1000*60*15;//15分钟
专用最终静态整数间隔5=1000*60*30;//30分钟
专用最终静态整数间隔6=1000*60*60;//1小时
专用最终静态整数间隔7=1000*60*1440;//一天
私有最终字符串主机名=”http://555.555.555.555";
私有最终字符串hostRequestName=“/yay.php”;
私有最终字符串hostwallpername=“/wallper/”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView)findViewById(R.id.TextView);
imageView=(imageView)findViewById(R.id.imageView);
rd1=(RadioButton)findViewById(R.id.RadioButton);
rd2=(RadioButton)findViewById(R.id.radioButton2);
rd3=(RadioButton)findViewById(R.id.radioButton3);
rd4=(RadioButton)findViewById(R.id.radioButton4);
rd5=(RadioButton)findViewById(R.id.radioButton5);
rd6=(RadioButton)findViewById(R.id.radioButton6);
rd7=(RadioButton)findViewById(R.id.radioButton7);
放射组=(放射组)findViewById(R.id.radioGroup);
mHandler=新处理程序();
btn=(按钮)findViewById(R.id.Button);
btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(rd1.isChecked()){
mHandlerTask=new Runnable(){
@凌驾
公开募捐{
新数据().execute();
mHandler.postDelayed(mHandlerTask,间隔);
startRepeatingTask();
}
};
}
}
void startRepeatingTask()
{
mHandlerTask.run();
}
});
}
私有类数据扩展异步任务{
@凌驾
受保护字符串doInBackground(无效…参数){
HttpURLConnection-urlConnection=null;
BufferedReader reader=null;
试一试{
URL=新URL(“http://555.555.555.555/yay.php");
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod(“POST”);
setRequestProperty(“内容类型”,“应用程序/x-www-form-urlencoded”);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
DataOutputStream wr=新的DataOutputStream(
urlConnection.getOutputStream());
write(“method=get_random_wallpaper.getBytes());
wr.flush();
wr.close();
InputStream InputStream=urlConnection.getInputStream();
StringBuffer=新的StringBuffer();
如果(inputStream==null){
返回null;
}
reader=新的BufferedReader(新的InputStreamReader(inputStream));
弦线;
而((line=reader.readLine())!=null){
buffer.append(第+行“\n”);
Log.d(“嘿,buffer.toString());
}
if(buffer.length()==0){
返回null;
}
forecastJsonStr=buffer.toString();
返回预报室JSONSTR;
}捕获(IOE异常){
Log.e(“占位符片段”,“错误”,e);
返回null;
}最后{
if(urlConnection!=null){
urlConnection.disconnect();
}
if(读卡器!=null){
试一试{
reader.close();
}捕获(最终IOE例外){
e、 printStackTrace();
}
}
}
}
@凌驾
受保护的void onPostExecute(最终字符串forecastJsonStr){
txt.setText(forecastJsonStr);
Thread Thread=新线程(new Runnable(){
@凌驾
公开募捐{
WallpaperManager=wallperManager.getInstance(getApplicationContext());
试一试{
位图结果=Picasso.with(getBaseContext())
.load(主机名+主机名+预测JSONSTR)
.get();
壁纸管理器。挫折图(结果);
}捕获(IOEX异常){
例如printStackTrace();
}
}
});
thread.start();
super.onPostExecute(forecastJsonStr);
}
}
}
创建一个变量来存储选择间隔。将mHandlerTask
和startRepeatingTask
方法移至onCreate
之外。在onClick
callstartRepeatingTask
方法中
参见下面的代码
private final static int SELECTED_INTERVAL = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(rd1.isChecked()) {
SELECTED_INTERVAL = INTERVAL;
} else if (rd2.isChecked()) {
SELECTED_INTERVAL = INTERVAL2;
}
stopRepeatingTask();
startRepeatingTask();
}
});
}
void startRepeatingTask() {
mHandlerTask.run();
}
void stopRepeatingTask() {
mHandler.removeCallbacks(null);
}
Runnable mHandlerTask = new Runnable() {
@Override
public void run() {
new WallpaperData().execute();
mHandler.postDelayed(mHandlerTask, SELECTED_INTERVAL);
}
};
@Override
public void onDestroy() {
super.onDestroy();
stopRepeatingTask();
}
创建一个变量来存储选择间隔。移动
mHandlerTask
和