android java中的Import.class文件
有没有其他方法可以从android中的同一个包中导入另一个Java类 我正在尝试将store.class导入SMSreceiver.class 在SMSreceiver.class中,我输入以下代码,store storingKey=new store 但是仍然无法调用store.class中的方法 这将是我的store.class编码android java中的Import.class文件,java,android,eclipse,Java,Android,Eclipse,有没有其他方法可以从android中的同一个包中导入另一个Java类 我正在尝试将store.class导入SMSreceiver.class 在SMSreceiver.class中,我输入以下代码,store storingKey=new store 但是仍然无法调用store.class中的方法 这将是我的store.class编码 public class store extends Activity{ public store(){ } public voi
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();
}
我的编码有问题吗
有什么帮助吗
罗布,这是我的另一门课
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();
}
}
}编辑:试试这个
要在另一个类中调用非静态方法,需要对该类的引用
store storeRef = new store(); // create new instance and set reference
storeRef.saveToFile(); // call the method
如果已经有对该类的引用,则不需要创建它。可以在方法调用中传递引用:
public void calledMethod(store storeRef) {
storeRef.saveToFile();
更新
改变
storeKey sk=新的storeKey;
到
商店sk=新商店
因为store是您的原始类名,而不是storeKey
您可能还需要声明,在使用该方法时,您正在尝试使用它。是的,我已经这样做了。但还是一样。我无法调用saveToFile方法..您可以粘贴其他类中的代码吗?然后,我将编辑我的答案,并说明它应该如何工作。对不起,罗伯,这里有一个输入错误,它的存储,而不是存储密钥。很抱歉,仍然有一个错误该错误是强制关闭还是在编译之前?如果是,错误是什么?在我调用这个方法之后,如果这个方法可以运行,Toast将显示字符串public.keyasd,对吗?sk.saveToFilepublic.key,str;Toast.makeTextcontext、public.keyasd、Toast.LENGTH\u SHORT.show;但它从来没有显示…这就是问题所在