首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在BroadcastReceiver中使用ProgressDialog

如何在BroadcastReceiver中使用ProgressDialog
EN

Stack Overflow用户
提问于 2016-03-13 01:34:52
回答 1查看 797关注 0票数 1

在BroadcastReceiver的extend活动中显示ProgressDialog时,我遇到了一个严重的问题。实际上,我想在每5秒内获取php文件中的数据。我使用AlarmManager来定义时间,并使用AsyncTask来获取托管的php数据。

这是我的AlarmDemo活动

代码语言:javascript
复制
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活动

代码语言:javascript
复制
public class RepeatingAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "availble now", Toast.LENGTH_SHORT).show();
}

}

这是我的AsyncTask活动

代码语言:javascript
复制
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新手

EN

回答 1

Stack Overflow用户

发布于 2016-03-13 01:48:51

试一试

代码语言:javascript
复制
public void onReceive(Context context, Intent intent) {
    ProgressDialog Dialog = new ProgressDialog(context);

    Toast.makeText(context, "available now", Toast.LENGTH_SHORT).show();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35961042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档