Android 如何在AsyncTask的onPostExecute方法中打开摄像头
我想在Android 如何在AsyncTask的onPostExecute方法中打开摄像头,android,android-asynctask,Android,Android Asynctask,我想在AsyncTask中打开相机,但是在doInBackgroundhook方法之后。 实际上,我正在从网络(servlet)检索数据,如果检索到的数据与条件匹配,我就设置了一个变量,比如intx=1;如果(x==1){camera.open();} 请建议我,怎么办 我写了一些代码 广播接收机 public class StartAtBootServiceReceiver extends BroadcastReceiver { public void onReceive(Contex
AsyncTask
中打开相机,但是在doInBackground
hook方法之后。
实际上,我正在从网络(servlet)检索数据,如果检索到的数据与条件匹配,我就设置了一个变量,比如intx=1;如果(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
}
公共类StartBootService扩展服务{
公共静态最终字符串url=”http://10.0.2.2:1010/junction/AsynLocInsertionServ";
字符串imei=“1234”;
字符串returnString=null;
返回的整数值=0;
专用静态最终int摄像机_请求=1888;
公共IBinder onBind(意向){
返回null;
}
@凌驾
public void onCreate(){
Log.d(“StartServiceAtBoot1”、“StartBootService Created1”);
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
d(“StartServiceAtBoot2”,“StartBootService--onStartCommand()”;
新建GetXMLTask().execute(url);//调用异步任务
返回开始时间;
//返回启动\u重新交付\u意图;
}
私有类GetXMLTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…URL){
字符串输出=null;
试一试{
DefaultHttpClient httpClient=新的DefaultHttpClient();
//ResponseHandler res=新的BasicResponseHandler();
HttpPost HttpPost=新的HttpPost(url);
试一试{
字符串Imei=“10101”;
字符串纬度=“333.33”;
字符串经度=“44.44”;
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“param1imei”),
(i),;
添加(新的BasicNameValuePair(“param2lati”),
纬度);
添加(新的BasicNameValuePair(“param3longi”),
经度);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse HttpResponse=httpClient.execute(httpPost);
//String response=httpClient.execute(httpPost,res);
HttpEntity HttpEntity=httpResponse.getEntity();
输出=EntityUtils.toString(httpEntity);
}//内试块末端
捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}//外部try块的结束
捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
返回输出;
}//getOutputFromUrl方法的结尾
@凌驾
受保护的void onPostExecute(字符串输出){
super.onPostExecute(输出);
Log.d(“在mysql\u onPost方法中长期存储完成”,
“”+output.toString());
如果(输出==1){
Intent Intent=新的Intent(“android.media.action.IMAGE_-CAPTURE”);
getApplicationContext().startActivityForResult(intent,0);
}
}//邮寄
}//GetXMLTask结束
}
将结果数据类型设置为int
并在doInBackground()的末尾返回1(如果匹配)
在onPostexecute(int result)时,1将被传递,在那里可以安全地启动相机。假设您从服务器获取值,它位于
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>
更多信息,请参见我们可以在
onPostexecute()
你能再发一些代码吗?我还没有代码。。有什么建议吗,先生?怎么做?它工作正常,但我有一个启动时启动的任务。。我有错误::类型my_StartBootService的startActivityForResult(Intent,int)方法未定义。myTask my_StartBootService是在启动时启动并完成固定报警时间的服务,,,,myTask是放置AsynTask的类。我刚刚在OnStartCommand()服务钩子方法中创建了mytask的对象。。。希望你明白我的意思。如果你有一个上下文,那么只需执行上下文即可。startActivityForResult(intent,0);else在asyntask构造函数中传递上下文
// global declaration
private static final int CAMERA_REQUEST = 1888;
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);