我的代码没有收到崩溃错误。当我运行应用程序并单击按钮时,应显示Toast,但事实并非如此。
这是我的IntentService
package com.example.flas;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.flas.JSONParser;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.Log;
import android.widget.Toast;
public class SampleIntentService extends IntentService {
public static final int DOWNLOAD_ERROR = 10;
public static final int DOWNLOAD_SUCCESS = 11;
String latbus;
String lonbus;
String latsta;
String Employernumber;
String lonsta;
String numrout;
String nomsta;
JSONParser jsonParser = new JSONParser();
private static final String url_product_detials = "http://********/get_loc_details2.php";
private static final String TAG_IDLOCBUS = "idlocbus";
private static final String TAG_BUSNUMBER = "BusNumber";
private static final String TAG_BUSLATITUDE = "BusLatitude";
private static final String TAG_BUSLONGITUDE = "Buslongitude";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCT = "product";
public SampleIntentService() {
super(SampleIntentService.class.getName());
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
String url = intent.getStringExtra("url");
final ResultReceiver receiver = intent.getParcelableExtra("receiver");
Bundle bundle = new Bundle();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", url));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(
url_product_detials, "GET", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
int success = json.getInt(TAG_SUCCESS);
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_PRODUCT); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
// product with this pid found
// display product data in EditText
String aa = product.getString(TAG_IDLOCBUS);
String bb = product.getString(TAG_BUSNUMBER);
String latb = product.getString(TAG_BUSLATITUDE);
String lonb = product.getString(TAG_BUSLONGITUDE);
bundle.putString("filePath", lonb + latb);
receiver.send(DOWNLOAD_SUCCESS, bundle);
} catch (Exception e) {
// TODO: handle exception
receiver.send(DOWNLOAD_ERROR, Bundle.EMPTY);
e.printStackTrace();
}
}
}
我的MainActivity
调用了我的IntentService
public class MainActivity extends Activity implements OnClickListener {
ProgressBar pd;
SampleResultReceiver resultReceiever;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultReceiever = new SampleResultReceiver(new Handler());
pd = (ProgressBar) findViewById(R.id.downloadPD);
}
private class SampleResultReceiver extends ResultReceiver {
public SampleResultReceiver(Handler handler) {
super(handler);
// TODO Auto-generated constructor stub
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
// TODO Auto-generated method stub
switch (resultCode) {
case SampleIntentService.DOWNLOAD_ERROR: {
Toast.makeText(getApplicationContext(), "error in download", Toast.LENGTH_SHORT).show();
pd.setVisibility(View.INVISIBLE);
}
break;
case SampleIntentService.DOWNLOAD_SUCCESS: {
String filePath = resultData.getString("filePath");
Toast.makeText(getApplicationContext(), "image download via IntentService is done", Toast.LENGTH_SHORT).show();
pd.setIndeterminate(false);
pd.setVisibility(View.INVISIBLE);
}
break;
}
super.onReceiveResult(resultCode, resultData);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent startIntent = new Intent(MainActivity.this, SampleIntentService.class);
startIntent.putExtra("receiver", resultReceiever);
String aaa = "5";
startIntent.putExtra("url", aaa);
startService(startIntent);
pd.setVisibility(View.VISIBLE);
pd.setIndeterminate(true);
}
public void onClick2(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "image download via IntentService is done", Toast.LENGTH_SHORT).show();
}
}
我的JSON类工作正常,我在另一个应用程序中尝试过,我没有错误。
在我的AndroidManifest.xml
...
我试过了:
<service android:name="com.example.pfecuspart.SampleIntentService"
android:exported="false">
<intent-filter>
<action android:name="org.example.android.myservicedemo.IService" />
</intent-filter>
</service>
我也试过这个:
<service android:name="com.example.pfecuspart.SampleIntentService"
android:enabled="true">
</service>
答案 0 :(得分:0)
我不认为onClick2正在做任何事情,它不会覆盖onClick。 你需要搬家吗?
Toast.makeText(getApplicationContext(),
"image download via IntentService is done",
Toast.LENGTH_SHORT).show();
从onClick2到onClick。
也许尝试用getApplicationContext()
MainActivity.this