別のJavaクラスのサーバーから完全な応答を受け取ったときにアクティビティを終了したい.
カテゴリ.java
主な活動です
public void onListItemClick(ListView parent, View v, int position, long id)
{
// Get the selected category id & name.
String catId = "";
String catName = "";
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
View view = ll.getChildAt(0);
if(view instanceof ListView) {
ListView lView = (ListView) view;
RowData rowData = (RowData) lView.getAdapter().getItem(position);
catId = rowData.mCatId;
catName = rowData.mTitle;
}
String url = "http://global.thinlenses.co.uk/virtualMirror/productlisting.php";
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "<category><Id>" + catId + "</Id>"
+ "<Data>" + "pList" + "</Data></category>";
if(getIntent().getBooleanExtra("VM", false))
new SpinnerHelper().serverCall(Category.this, "ProductListVM", url, xml, catName);
else
new SpinnerHelper().serverCall(Category.this, "ProductList", url, xml, catName);
}
SpinnerHelper.java 通常のクラスはアクティビティではありません
void serverCall(Context context, final String target, final String url, final String xml, String extraValue)
{
try {
this.context = context;
this.target = target;
this.extraValue = extraValue;
ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(conMan.getActiveNetworkInfo() != null && conMan.getActiveNetworkInfo().isConnected())
{
dialog = new ProgressDialog(context);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setCancelable(true);
dialog.setTitle(R.string.app_name);
dialog.setMessage(context.getString(R.string.progressing));
dialog.show();
// Create a thread for updating the spinner.
Thread progressThread = new Thread(new Runnable() {
public void run() {
try {
while(!isExecuted) {
Thread.sleep(1000); // Wait 1000ms between each update.
handler.sendMessage(handler.obtainMessage()); // Active the update handler.
}
}
catch (InterruptedException e) {
Log.e("VM", "Exception while spinner is updating at: " + e.getMessage());
}
}
});
progressThread.start();
// Create a thread to get response from server.
Thread registerThread = new Thread(new Runnable() {
public void run()
{
// Get response XML from server and parse it.
String resXML = new Connection().getResponse(url, xml);
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(resXML);
if(doc != null)
{
if(target.equals("Category")) {
NodeList nodeList = doc.getElementsByTagName("Category");
name = new String[nodeList.getLength()];
id = new String[nodeList.getLength()];
// Fetch all node values and store into arrays.
for(int index = 0; index < nodeList.getLength(); index++)
{
Element element = (Element) nodeList.item(index);
id[index] = parser.getValue(element, "id");
name[index] = parser.getValue(element,"CategoryName");
}
result = "yes";
}
}
}
このハンドラーは、サーバーから完全な応答を受信したときに呼び出されます。
Handler handler = new Handler()
{
public void handleMessage(Message message)
{
if(isExecuted)
{
dialog.dismiss();
isExecuted = false;
if(result.equals("yes"))
{
/////HERE i want to start the next activity and finish the Category(previous activity).
}