2015-04-27 42 views
-1

我的代码没有收到崩溃错误。当我运行应用程序并单击按钮时,Toast应显示,但不显示。IntentService不起作用android

这里是我的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类工作得很好,我想它在另一个App和我没有错误。

在我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

这就是概率的intentservice不表兄弟姐妹当我点击按钮(的onClick)工作敬酒不显示,所以我创建另一个按钮onClick2和更换getApplicationContext()当我按下它..吐司显示.. –

+0

我的问题在onReceiveResult为什么在onReceiveResult吐司不工作:'( –

回答

0

我不认为onClick2是做任何事情,它不是重写的onClick 。 你需要移动;

Toast.makeText(getApplicationContext(), 
    "image download via IntentService is done", 
    Toast.LENGTH_SHORT).show(); 

从onClick2到onClick。 也许尝试与MainActivity.this

+0

onClick2没有做任何事情因为我只是试图确保getApplicationContext()是正确的,所以它的工作原理,但在其他吐司的问题 –

+0

其他吐司在onClick()? – matty357

+0

做一个系统.out.println(发送resultCode);看看代码实际上是什么,以及它是否真正匹配它应该是什么。 – matty357