在BroadcastReceiver的extend活动中显示ProgressDialog时,我遇到了一个严重的问题。实际上,我想在每5秒内获取php文件中的数据。我使用AlarmManager来定义时间,并使用AsyncTask来获取托管的php数据。
这是我的AlarmDemo活动
public class AlarmDemo extends Activity {
Toast mToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_demo);
// repeated alerm
Intent intent = new Intent(AlarmDemo.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmDemo.this, 0,
intent, 0);
// We want the alarm to go off 5 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 5 * 1000;
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
5 * 1000, sender);
// Tell the user about what we did.
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(AlarmDemo.this, "Rescheduled",
Toast.LENGTH_LONG);
mToast.show();
// end repeatdalarm
}}
这是我的RepeatingAlarm活动
public class RepeatingAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "availble now", Toast.LENGTH_SHORT).show();
}}
这是我的AsyncTask活动
public class AsyncronoustaskAndroidExample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.asyncronoustask_android_example);
String serverURL = "http://androidexample.com/media/webservice/getPage.php";
new LongOperation().execute(serverURL);
}
// Class with extends AsyncTask class
private class LongOperation extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(
AsyncronoustaskAndroidExample.this);
TextView uiUpdate = (TextView) findViewById(R.id.output);
protected void onPreExecute() {
// NOTE: You can call UI Element here.
// UI Element
uiUpdate.setText("Output : ");
Dialog.setMessage("Downloading source..");
Dialog.show();
}
// Call after onPreExecute method
protected Void doInBackground(String... urls) {
try {
// Server url call by GET method
HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);
} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
// NOTE: You can call UI Element here.
// Close progress dialog
Dialog.dismiss();
if (Error != null) {
uiUpdate.setText("Output : " + Error);
} else {
uiUpdate.setText("Output : " + Content);
}
}
}}
现在,当我分别使用AsyncTask和报警管理器代码时,它是work find,但当我尝试将它们组合到onReceive方法中时,ProgressDialog中出现错误。它说“构造函数ProgressDialog(RepeatingAlarm)是未定义的”。你能帮帮我吗,因为我是Android新手
发布于 2016-03-13 01:48:51
试一试
public void onReceive(Context context, Intent intent) {
ProgressDialog Dialog = new ProgressDialog(context);
Toast.makeText(context, "available now", Toast.LENGTH_SHORT).show();
}https://stackoverflow.com/questions/35961042
复制相似问题