【发布时间】:2016-10-31 08:11:52
【问题描述】:
我在 mainactivty 上有 7 个单选按钮,每个按钮都需要不同的时间(1 分钟、5 分钟、10 分钟……),我有异步任务,我正在从服务器(php)调用图片,所以我想要当用户选择第一个单选按钮(1 分钟)时,异步任务每 1 分钟执行一次,我尝试了 radiobutton.ischecked 和 puttet 处理程序,但没有成功 我的服务器正在工作,我正在收到响应,但我无法使用此设置执行异步任务来设置壁纸,它查看了我的代码
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);
}
}
}
【问题讨论】:
-
您可能也对运行多个 asyncTask 感兴趣。如果您的异步任务重叠,您应该查看
executeOnExecutor()方法:android-er.blogspot.de/2014/04/… 和此处:stackoverflow.com/questions/4068984/… -
也许你可以使用 AlarmManager 来达到这个目的
标签: android android-asynctask handler