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

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

问题描述

我正在做一个简单的应用程序,从 editText 获取数据,然后它得到文本并发送到java类(在类中我需要它来转换字符串到utf8编码)。

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?

这是我的UTFencoder类:

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;
}

}


推荐答案

选项1:在Android中,您可以通过Intent或Intent,后跟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";
    }
 }

/ p>

And fetch this value in your java class like:

  String value = A._utfValue ;

选项3:您可以使用SharedPreference保存值,并从其他类中获取

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天全站免登陆