2016-03-12 44 views
1

在BroadcastReceiver中的擴展活動中顯示ProgressDialog時,出現嚴重問題。 實際上,我想每5秒鐘內在php文件中獲取數據。我使用AlarmManager來定義時間和 使用AsyncTask來獲得託管的PHP數據。如何在BroadcastReceiver中使用ProgressDialog

這是我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和告警管理代碼分別是工作找,但是當我試圖聯合那些在的onReceive方法,存在ProgressDialog一個錯誤。它表示「構造函數ProgressDialog(RepeatingAlarm)未定義」。你能幫幫我,因爲我在我的Android新

+0

不要告訴我們「存在錯誤」。告訴我們錯誤是什麼。 –

+0

@GabeSechan yes錯誤是「構造函數ProgressDialog(RepeatingAlarm)未定義」 – Kavindu

+0

BroadcastReceiver不是上下文。您無法將其傳遞到預期上下文的位置。如果你需要一個Context,你需要在onReceive中傳遞給你的Context。 –

回答

0

嘗試

public void onReceive(Context context, Intent intent) { 
    ProgressDialog Dialog = new ProgressDialog(context); 

    Toast.makeText(context, "available now", Toast.LENGTH_SHORT).show(); 
} 
+0

對不起,這不適合我。 – Kavindu

相關問題