简体   繁体   English

在 android java 中导入.class 文件

[英]Import .class file in android java

is there any other way to import another Java class from the same package in android?有没有其他方法可以从 ZC31B32364CE19CA8ZFCD150CE 中的同一个 package 导入另一个 Java class

I'm trying to import store.class to SMSreceiver.class in SMSreceiver.class, i type in this code, store storingKey = new store();我正在尝试将 store.class 导入 SMSreceiver.class 中的 SMSreceiver.class,我输入此代码,store storageKey = new store();

but still cannot call the methods in store.class但仍然无法调用 store.class 中的方法

this will be my store.class coding.这将是我的商店。class 编码。

public class store extends Activity{
    public store(){

    }

     public void saveToFile(String filename, String sms) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException{
            OutputStreamWriter out = new OutputStreamWriter(openFileOutput(filename, Context.MODE_APPEND));
            out.write(sms);
            out.close();            
    }

is there any problem with my coding?我的编码有什么问题吗?

any help?有什么帮助吗?

Rob, this is my another class Rob,这是我的另一个 class

public class storePubKey extends BroadcastReceiver
{
    Activity ac = new Activity();
    store sk = new store();
    public void onReceive(Context context, Intent intent)
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";            
    if (bundle != null)
    {
        try{
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str = msgs[i].getMessageBody().toString();
            Toast.makeText(context, "f", Toast.LENGTH_SHORT).show();
            sk.saveToFile("public.key", str);
            Toast.makeText(context,("public.keyasd"), Toast.LENGTH_SHORT).show();

        }
        }catch(Exception e){}

        }
        //---display the new SMS message---
        try {
            //Toast.makeText(context,str, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}   

} }

EDIT: Try this编辑:试试这个

To call a non-static method in another class, you need a reference to that class.要在另一个 class 中调用非静态方法,您需要对该 class 的引用。

store storeRef = new store(); // create new instance and set reference
storeRef.saveToFile(); // call the method

If you already have a reference to that class, you don't need to create it.如果您已经引用了该 class,则无需创建它。 A reference can be passed in a method call:可以在方法调用中传递引用:

public void calledMethod(store storeRef) {
storeRef.saveToFile();

UPDATE更新

Change storeKey sk = new storeKey();更改storeKey sk = new storeKey(); to store sk = new store();存储 sk = new store();

Since store is your original class name, not storeKey.由于 store 是您原来的 class 名称,而不是 storeKey。

You also may need to declare that withing the method you are trying to use it in.您可能还需要声明您尝试使用它的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM