如何从Android的活动将数据发送到java类? [英] how to send data from android Activity to java class?

查看:137
本文介绍了如何从Android的活动将数据发送到java类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个简单的应用程序,获取数据 EDITTEXT 则获得该文本发送到Java类(在课堂上,我需要它来字符串转换为utf8编码)。我该怎么做,从活动的发送数据,我怎么能接受它的Java类?

I am doing a simple app that get data from editText then it get that text and send to java class ( in the class I need it to convert string to utf8 encoding). What shall I do to send data from the activity and how can I receive it in the java class?

这是我的UTFen codeR类:

this is my UTFencoder Class:

public class UTFencoder {

public void main(String[] args) {

      String testString = encodeStringToUTF8("this is my app ... eskadenia");
      System.out.println(testString);
}

public static String encodeStringToUTF8(String stringToEncode) {
    String newValue = "";
    List<String> charactersCode = new ArrayList<String>();
    try {
        byte[] stringBytes = stringToEncode.getBytes("UTF-8");
        for (int i = 0; i < stringBytes.length; i++) {
            //System.out.println("utf value is " + stringBytes[i]);
            String value = String.valueOf(stringBytes[i]);

            int no = Integer.parseInt(value);
            String hex = Integer.toHexString(no);
            //System.out.println("Hex value is " + hex);
            for (int j = 0; j < hex.length(); j++) {
                charactersCode.add(String.valueOf(hex.charAt(j)));                  
            } 

        }
    } catch (UnsupportedEncodingException e) {
        System.out.println("Cannot encode " + stringToEncode
                + " to UTF-8, UnsupportedEncodingException");
    }

Iterator iterator =charactersCode.iterator();

while(iterator.hasNext()){
    Object element= iterator.next();
    System.out.print(element+" ");




}

    return newValue;
}

}

在此先感谢。

Thanks in advance.

推荐答案

选项1:在Android中,你可以通过意图或意图之后Bundle.Like发送数据

Option 1:In android you can send data through Intent or Intent followed by Bundle.Like

Intent i = new Intent(current_class.this, linked_class.class);
   i.putextra("Key", value);

和得到的值(假设字符串值)其它类,如:

And get the value(suppose string value) in another class like:

  String value = getIntent.getExtra("String key which you used when send value");

但是,对于你的问题,你是我不知道它可以帮助或没有,但你可以在你的活动课使用全局静态变量,并指定要发送你的方法..像

But for your question you as I am not sure it helps or not, but you can use a global static variable in your activity class and assign which value you want to send in your method.. like

选项2:

 class A{

   public static String _utfValue = "";

   void sendValue(){
       _utfValue  = "some value";
    }
 }

而在你的Java类中获取该值,如:

And fetch this value in your java class like:

  String value = A._utfValue ;

选项3:您可以使用共享preference保存价值和其他类得到它

Option 3: You can use SharedPreference to save the value and get it from other class.

选择4:您可以使用一个静态方法与一些返回值,并通过类名Java类获取方法

Option 4: You can use a static method with some return value and fetch the method in your java class through class name.

所有的选项是粗糙这里。所以只是检查这一点,希望这些人会帮你。

All the options are rough here. So just check this ,hope one of these will help you .

这篇关于如何从Android的活动将数据发送到java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆