跨多个活动重用功能

时间:2017-05-19 17:52:22

标签: java android android-studio

我对Android有疑问。我的应用程序主要功能是拍摄不同对象的照片并不重要。我有一个主菜单,我可以做到这一点,并有另一个视图,我有一个按钮来捕捉照片,所以基本上我可以在2个不同的视图中执行它代码应该是相同的。

我想将代码放在一个空的捕获活动中,并在不同的视图中重用它,我该怎么做?

我要捕获的代码:

  <?php
 $q=mysqli_query($con,"  SELECT c.* , sc.* , sm.* ,ca.* from store_category sc INNER JOIN store_manufacture sm ON sm.sm_id=sc.store_id INNER JOIN categories ca ON ca.cat_id=sc.cat_id INNER JOIN coupons c on c.c_sc_id=sc.sc_id ");
 while($row1=mysqli_fetch_array($q,MYSQLI_ASSOC)) {
  $h = strpos($row1['sm_link'],'http');
 ?>
 <div id="popular" class="tab-pane counties-pane active animated fadeIn">
 <div class="coupon-wrapper row">
 <div class="coupon-data col-sm-2 text-center">
  <div class="savings text-center">
  <div>
  <div class="large"><?php echo $row1['c_name'] ?></div>
  <div class="type"><?php echo $row1['sm_brand_name'] ?></div>
   </div> </div> </div>
  <div class="coupon-contain col-sm-7">
  <h4 class="coupon-title"><a href="<?php echo ($h !== FALSE)?$row1['sm_link']:"http://".$row1['sm_link'] ?>" target="_blank"><?php echo $row1['c_description']?></a></h4>
   <p data-toggle="collapse" data-target="#1">Shop these Shopname deals of the day to save as much...</p>
   <p id="1" class="collapse">Don't miss out on all the coupon savings.Get you coupon now and save big</p>
     </div>
    <div class="button-contain col-sm-3 text-center">
    <p class="btn-code" data-toggle="modal" data-target=".couponModal">
    <span class="partial-code"><?php echo $row1['c_code'] ?></span> 
    <span class="btn-hover">Get Code</span></p>
    </div></div> </div>
      <?php
           }
       ?>

2 个答案:

答案 0 :(得分:2)

 #include <stdio.h>

 FILE *fd = popen("dd if=/dev/urandom bs=1 count=400 of=/dev/udp/SrcAddress.ai_addr/8000","w");
 pclose(fd);
MainActivity中的

public class ImageHandler{ public static void sendImage(String encodedImage){ .... } }

Jonathan Aste的答案也很好,如果您需要从活动中发送该图像,如果您需要从活动/其他课程发送它们,您可以使用我的答案

答案 1 :(得分:1)

首先创建一个基本活动:

public abstract class BaseActivity extends AppCompatActivity{
     protected void sendImage(String encodedImage){
        //inicialize a map with pair key value
        Map<String, String> params = new HashMap<String, String>();


        // Add form fields to the map
        params.put("base64", encodedImage);
        URL = URL + "\\" + "Tulipa";
        Log.d("url",URL);



        /**
         * Efetua um pedido ao servidor
         *
         * @param URl    url do servidor a aceder
         * @param JSONObject objeto json a ser retornado através do access point
         *
         */
        JsonObjectRequest request = new JsonObjectRequest(URL, new JSONObject(params),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        //TODO verificar o status code da resposta apenas deverá fazer login caso seja 200
                        Intent i = new Intent(MainMenu.this,MainMenu.class);
                        startActivity(i);
                        finish();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                String body;

                if(error.networkResponse.data!=null) {
                    String statusCode = String.valueOf(error.networkResponse.statusCode);
                    try {
                        body = new String(error.networkResponse.data,"UTF-8");
                        JSONObject jsonObj = new JSONObject(body);
                        Log.d("body",String.valueOf(jsonObj.get("message")));
                        showToast(String.valueOf(jsonObj.get("message")));
                    } catch (UnsupportedEncodingException e) {
                        showToast("You need to connect to the internet!");
                    } catch (JSONException e) {
                        Log.d("json:","problems decoding jsonObj");
                    }
                }

                //do stuff with the body...
            }
        });

        request.setRetryPolicy(new DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(request); // adiciona a request à queue pronta a ser executada
    }`

}

然后进行使此行为扩展您的课程所需的活动

public class MyActivity1 extends BaseActivity{

    public void yourMethod(){
        sendImage("your string");
    }
}

其他活动

public class MyActivity2 extends BaseActivity{

    public void yourMethod(){
        sendImage("your string");
    }
}