我想在AsyncTask
中打開相機,但在doInBackground
之後的時候掛鉤的方法。 實際上我從網絡(servlet)檢索數據,如果檢索到的數據與條件匹配,我設置了一個變量說int x=1 ; if (x==1) { camera.open() ;}
如何打開相機中的onPostExecute方法的AsyncTask
請建議我,該怎麼辦?
我已經寫了一些代碼
廣播接收器
public class StartAtBootServiceReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// Start periodic service.
Calendar cal = Calendar.getInstance();
Intent srvIntent = new Intent(context, StartAtBootService.class); // NoLopper
PendingIntent pIntent = PendingIntent.getService(context, 0, srvIntent, 0);
// Use context argument to access service
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Repeat every 5 seconds
alarm.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(),
10000,
pIntent
);
}
}
這是服務
public class StartAtBootService extends Service {
public static final String url = "http://10.0.2.2:1010/junction/AsynLocInsertionServ";
String imei = "1234";
String returnString = null;
Integer returnedValue = 0;
private static final int CAMERA_REQUEST = 1888;
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.d("StartServiceAtBoot1", "StartAtBootService Created1");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("StartServiceAtBoot2", "StartAtBootService -- onStartCommand()");
new GetXMLTask().execute(url); // calls the AsyncTask
return START_STICKY;
// return START_REDELIVER_INTENT ;
}
private class GetXMLTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String output = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
// ResponseHandler<String> res = new BasicResponseHandler();
HttpPost httpPost = new HttpPost(url);
try {
String Imei = "10101";
String Latitude = "333.33";
String Longitude = "44.44";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("param1imei",
Imei));
nameValuePairs.add(new BasicNameValuePair("param2lati",
Latitude));
nameValuePairs.add(new BasicNameValuePair("param3longi",
Longitude));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
// String response = httpClient.execute(httpPost, res);
HttpEntity httpEntity = httpResponse.getEntity();
output = EntityUtils.toString(httpEntity);
}// end of inner try block
catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} // end of outer try block
catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
return output;
} // end of getOutputFromUrl method
@Override
protected void onPostExecute(String output) {
super.onPostExecute(output);
Log.d("done LAtlong stored in mysql_onPost method",
" " + output.toString());
if (output == 1) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
getApplicationContext().startActivityForResult(intent, 0);
}
} // onpost
} // end of GetXMLTask
}
這個代碼,你能不能請張貼更多的代碼。 – RvdK
我還沒有編碼..任何建議先生?這個怎麼做? – Reshi