如何在AsyncTask的onPostExecute方法中打开相机

时间:2014-05-06 07:55:46

标签: android android-asynctask

我想在AsyncTask中打开相机,但是在doInBackground挂钩方法之后。 实际上我正在从网络(servlet)中检索数据,如果检索到的数据符合条件,我设置了一个变量说int x=1 ; if (x==1) { camera.open() ;}

请建议我,该怎么办?

我写了一些代码

广播接收器

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
}

3 个答案:

答案 0 :(得分:0)

将结果数据类型设置为int 并在doInBackground()

结束时返回1(如果匹配)

在onPostexecute(int result)中,1将被传递,并且可以安全启动摄像头。

答案 1 :(得分:0)

假设您从服务器获取价值,它位于x然后执行

onPostExecute(void result) {

if(x==1) {
 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
 startActivityForResult(intent, 0); 
}

}

并在清单文件中添加使用权限。

 <uses-permission android:name="android.permission.CAMERA"></uses-permission>

了解更多信息,read here

答案 2 :(得分:0)

我们可以在onPostexecute()

上编写此代码
// global declaration
private static final int CAMERA_REQUEST = 1888;

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);   
startActivityForResult(cameraIntent, CAMERA_REQUEST);
相关问题