我想在后台同步我的应用程序...并使用基于通知的方法。我有一个广播接收器,服务,活动和另一个带有Receiver的Context参考的java类。收到推送通知后...接收方呼叫服务,服务调用BackgroundSync。 BackgroundSync类执行一些齐射网络调用,并且成功时,它将数据存储在异步任务内的本地数据库中。现在在这个类的onPostExecute()中..我想在activity上调用一个方法来显示一个对话框。
//Inside service. This method is called from receiver on receiving push notification
public static void syncDataFromServer(Context context) {
Toast.makeText(context, "Data Sync Started", Toast.LENGTH_LONG).show();
try {
new BackgroundSync(context);
Log.d("SYNC", "Called BackgroundSync Class with Context " + context);
} catch (Exception e) {
Log.d("SYNC", "Exception BackgroundSync Class :" + e.toString());
}
}
这是后台同步类:
public class BackgroundSync implements IRequestCallback {
private Context context;
BackgroundSync(Context context) {
this.context = context;
fetchRetailers(context);
}
private class InsertTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
DBAdapter dbAdapter = new DBAdapter(context);
dbAdapter.open();
dbAdapter.insertData(params[0], false);
dbAdapter.close();
Log.d("SYNC", "Retailers saved to Local database in Async Task of BackgroundSync");
} catch (IllegalAccessException e) {
Log.d("SYNC", "Exception "+e.toString());
}
return "";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
MainActivity.notifyBackgroundSyncDone(context);
}
}
@Override
public void fetchRetailerResponseAPI(Object response, String str) {
try {
new InsertTask().execute(response.toString());
Log.d("SYNC", "Fetch Retailer API Success");
} catch (Exception e) {
Log.d("SYNC", "Exception "+e.toString());
}
}
}
public void fetchRetailers(Context context) {
try {
BaseData baseData = new BaseData();
baseData.setApi(Constants.VIEW_RETAILERS_API);
baseData.setUrl(CommonUtil.getBaseUrl() + "distributors/" + Integer.parseInt(Prefs.getId(context)) + "/retailers");
baseData.setPostOrGet(Request.Method.GET);
Task task = new Task(this, context);
task.getData(baseData);
Log.d("SYNC", "Fetching retailers from Server");
} catch (Exception e) {
Log.d("SYNC", "Exception "+e.toString());
}
}
}
一切都运作良好......我能够在我的活动中展示祝酒......但是我无法展示对话。看起来各种情况都存在一些问题。
这是活动中的方法:
public static void notifyBackgroundSyncDone(Context context){
Toast.makeText(context, "Data Sync Finished", Toast.LENGTH_LONG).show();
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
答案 0 :(得分:1)
AlertDialog
,因此他们的Builder
需要Activity Context
来绘制与GUI相关的设置和资源。您在Context
中传递的BroadcastReceiver
是Application Context
,其中没有必要的资源,会导致AlertDialog
创建失败。
如果您希望直接从AlertDialog
或Service
展示与BroadcastReceiver
非常相似的内容,则可以使用{{1}创建Activity
}主题,并以Dialog
开头,并附加任何必要的数据作为附加内容。
答案 1 :(得分:0)
执行此操作并尝试显示您的对话框
public static void notifyBackgroundSyncDone(Context context){
Toast.makeText(context, "Data Sync Finished", Toast.LENGTH_LONG).show();
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// dialog.cancel();
}
});
builder1.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}