Java tutorial
/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.grass.caishi.cc.service; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Map; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.json.JSONException; import org.json.JSONObject; import com.grass.caishi.cc.Constant; import com.grass.caishi.cc.domain.SendBean; import com.grass.caishi.cc.utils.CommonUtils; import com.grass.caishi.cc.utils.HttpRestClient; import com.grass.caishi.cc.utils.JsonToMapList; import com.loopj.android.http.BaseJsonHttpResponseHandler; import com.loopj.android.http.RequestParams; import com.loopj.android.http.TextHttpResponseHandler; import android.app.Service; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; import android.os.Looper; import android.os.Message; import android.widget.Toast; public class BaseIntentService extends Service { private volatile Looper mServiceLooper; private static volatile ServiceHandler mServiceHandler; private String mName = "BaseIntentService"; public static String BroadCastName = "com.grassweb.caishi.toupiao"; public static final int UP_LOANDING = 1, UP_FINSH = 2, UP_ADD = 3, UP_DEL = 4, UP_CLEAR = 5; private Intent intent; // private BroadCastListen broadcase; // public static ArrayList<SendBean> filelist = new ArrayList<SendBean>(); private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { doInbackground(msg); } } /** * Creates an IntentService. Invoked by your subclass's constructor. * * @param name * Used to name the worker thread, important only for debugging. */ // public BaseIntentService(Context context){ // super(context); // } // public class BroadCastListen extends BroadcastReceiver{ // @Override // public void onReceive(Context context, Intent intent) { // // TODO Auto-generated method stub // int ddo=intent.getIntExtra("do", 0); // int fileid=intent.getIntExtra("imageId", 0); // String str=intent.getAction(); // if(str.equals(broadcase)&&ddo==UP_DEL&&fileid!=0){ // mServiceHandler.removeMessages(fileid); // for (int i = 0; i < filelist.size(); i++) { // ImageItem fu=filelist.get(i); // if(fu.imageId==fileid){ // filelist.remove(i); // intent.putExtra("do", UP_DEL); // sendBroadcast(intent); // break; // } // } // } // } // } @Override public void onCreate() { super.onCreate(); // filelist.clear(); intent = new Intent(BroadCastName); // broadcase=new BroadCastListen(); // IntentFilter filter=new IntentFilter(BroadCastName); // registerReceiver(broadcase, filter); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } public void removeIntent(int what) { if (mServiceHandler.hasMessages(what)) mServiceHandler.removeMessages(what); } /** * You should not override this method for your IntentService. Instead, * override {@link #onHandleIntent}, which the system calls when the * IntentService receives a start request. * * @see android.app.Service#onStartCommand */ @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null) { int ddo = intent.getIntExtra("do", 0); if (ddo != 0) { SendBean send = (SendBean) intent.getSerializableExtra("send"); Message msg = mServiceHandler.obtainMessage(); msg.arg1 = send.getSend_id(); msg.what = send.getSend_id(); Bundle b = new Bundle(); b.putSerializable("send", send); msg.setData(b); // filelist.add(send); mServiceHandler.sendMessage(msg); intent.putExtra("do", UP_ADD); sendBroadcast(intent); } } return START_NOT_STICKY; } @Override public void onDestroy() { intent.putExtra("progress", 100); intent.putExtra("do", UP_CLEAR); sendBroadcast(intent); mServiceLooper.quit(); } /** * Unless you provide binding for your service, you don't need to implement * this method, because the default implementation returns null. * * @see android.app.Service#onBind */ @Override public IBinder onBind(Intent intent) { return null; } long totalSize; public void doInbackground(Message msg) { Bundle b = msg.getData(); SendBean send = (SendBean) b.getSerializable("send"); RequestParams params = new RequestParams(); params.put("title", send.getTitle()); params.put("text", send.getText()); params.put("act_id", send.getAct_id()); params.put("iss_next", send.getIss_next()); ArrayList<String> images = send.getImg_list(); if (images != null && images.size() > 0) { for (int i = 0; i < images.size(); i++) { File file = new File(images.get(i)); try { params.put("img" + i, file, "image/jpeg"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } HttpRestClient.post(Constant.SEND_MATCH_DO, params, new TextHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, String responseString) { // TODO Auto-generated method stub if (CommonUtils.isNullOrEmpty(responseString)) { Toast.makeText(getApplicationContext(), "???", Toast.LENGTH_SHORT).show(); return; } Map<String, Object> m = JsonToMapList.getMap(responseString); if (m != null && !CommonUtils.isNullOrEmpty(m.get("data")) && m.get("ok").toString().equals("1")) { intent.putExtra("progress", 100); intent.putExtra("do", UP_FINSH); intent.putExtra("data", m.get("data").toString()); sendBroadcast(intent); } // filelist.remove(0); // if (filelist.size() == 0) { // intent.putExtra("progress", 100); // intent.putExtra("do", UP_CLEAR); // sendBroadcast(intent); // } } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "?", Toast.LENGTH_SHORT).show(); } }); } }