使用AsyncTask在后台服务内部添加android进度对话框,获取致命异常 [英] Adding android progress dialog inside Background service with AsyncTask,Getting FATAL Exception
问题描述
-
我会每10分钟从计划的服务中调用 Asynctask .
在运行服务时,进度对话框从 OnpreExecute 获取异常.
while running the Service, Progress dialog getting Exception from OnpreExecute.
错误:
FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:594)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
警报管理器,每5分钟调用一次服务
/*Alarm manager Service for From Server*/
private void setServerFetch() {
// for to Server to GPS PING
Intent myIntent1 = new Intent(LoginPage.this, AlarmService.class);
pendingintent1 = PendingIntent.getService(LoginPage.this, 1111, myIntent1, 0);
AlarmManager alarmManager5 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());
calendar1.add(Calendar.SECOND, 1);
alarmManager5.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingintent1);
alarmManager5.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), 300 * 1000, pendingintent1);
}
从服务启动中调用 AsyncTask
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
try
{
Asynctask_Incident task=new Asynctask_Incident();
task=new();
}
catch (Exception e)
{
e.printStackTrace();
Log.i("PING", "EXCEPTION in reading Data from Web Async task ONstart.!");
}
}
Asynctask类 onStart 方法
Asynctask Class onStart Method
public class Asynctask_Incident extends AsyncTask<String, Void, Void>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!pDialog.isShowing())
{
pDialog = new ProgressDialog(appContext);
pDialog.setCanceledOnTouchOutside(false);
pDialog.setCancelable(false);
pDialog.setMessage("Please Wait Updating Data From...");
pDialog.show();
}
}
});
}
@Override
protected Void doInBackground(String... params)
{
try {
getAPICall();
} catch (Exception e) {
e.printStackTrace();
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid)
{
super.onPostExecute(aVoid);
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
}
帮助我解决此问题.
推荐答案
实际上,您不能从服务启动进度对话框,因为它需要活动上下文而不是应用程序上下文,在您的情况下,该上下文将为空.
Actually you can't start a progress dialog from a service, because it needs the activity context not application context which come to be null in your case.
更多信息在这里: link1 , link2 和 link3
More info here: link1 , link2 and link3
如果要基于服务操作触发进度对话框,可以使用观察者设计模式,请在此处.
If you want to trigger progress dialog based on service action, you may use Observer design patter, look here.
更新: 如果您的应用正在运行,则可以使用Handler并每5分钟运行一次.
Update: If your app is running, you can use Handler and run it each 5 minutes.
这是一个完整的示例:
public class TestActivity extends AppCompatActivity {
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//
new Asynctask_Incident(TestActivity.this).execute("url");
handler.postDelayed(this, 5 * DateUtils.MINUTE_IN_MILLIS);
}
}, 0);
}
public class Asynctask_Incident extends AsyncTask<String, Void, Void> {
ProgressDialog pDialog;
Context appContext;
public Asynctask_Incident(Context ctx) {
appContext = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(appContext);
pDialog.setCanceledOnTouchOutside(false);
pDialog.setCancelable(false);
pDialog.setMessage("Please Wait Updating Data From...");
pDialog.show();
}
@Override
protected Void doInBackground(String... params) {
try {
getAPICall();
} catch (Exception e) {
e.printStackTrace();
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
return null;
}
private void getAPICall() {
//5 seconds delay for test, you can put your code here
try {
Thread.sleep(5 * DateUtils.SECOND_IN_MILLIS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
}
}
这篇关于使用AsyncTask在后台服务内部添加android进度对话框,获取致命异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!