更新时间:2024-10-25 13:12:29点击:
大家好,今天小编关注到一个比较有意思的话题,就是关于j*ascript没有链表的问题,于是小编就整理了1个相关介绍j*ascript没有链表的解答,让我们一起看看吧。
基于消息的通信机制 Intent ---boudle ,extra
数据类型有限,比如遇到不可序列化的数据Bitmap,InputStream, 或者LinkList链表等等数据类型就不太好用。
2. 利用static静态数据,public static成员变量;
3.基于外部存储的传输, File/Preference/ Sqlite ,如果要针对第三方应用需要Content Provider
4.基于IPC的通信机制context 与Service之间的传输,如Activity与Service之间的通信,定义AIDL接口文件。
5. 基于***lication Context
这里面我觉得第五种方***更具普适性,在网上找了篇讲解得好的文章,原文如下:
在Android中使用Intent在两个Activity间传递数据时,只能是基本类型数据,或者是序列化对象。Intent是一种基于消息的进程内和进程间通信模型,当我们需要在我们应用程序内部,多个Activity间进行复杂数据对象共享交互时,使用Intent就显得很不方便。此时,我们就需要一种数据共享的机制来实现。当然,直接使用j*a语言中的静态变量是可以的,但在Android中有更为优雅的实现方式。
The more general problem you are encountering is how to s*e stateacross several Activities and all parts of your ***lication. A staticvariable (for instance, a singleton) is a common J*a way of achievingthis. I h*e found however, that a more elegant way in Android is toassociate your state with the ***lication context.
--如想在整个应用中使用,在j*a中一般是使用静态变量,而在android中有个更优雅的方式是使用***lication context。
As you know, each Activity is also a Context, which is informationabout its execution environment in the broadest sense. Your ***licationalso has a context, and Android guarantees that it will exist as asingle instance across your ***lication.
--每个Activity 都是Context,其包含了其运行时的一些状态,android保证了其是single instance的。
The way to do this is to create your own subclass of android.***.***lication,and then specify that class in the ***lication t* in your manifest.Now Android will automatically create an instance of that class andmake it *ailable for your entire ***lication. You can access it fromany context using the Context.get***licationContext() method (Activityalso provides a method get***lication() which has the exact sameeffect):
--方***是创建一个属于***自己的android.***.***lication的子类,然后在manifest中申明一下这个类,这是android就为此建立一个全局可用的实例,***可以在其他任何地方使用Context.get***licationContext()方***获取这个实例,进而获取其中的状态(变量)。
[js] view plaincopyprint?
[js] view plaincopyprint?
class My*** extends ***lication {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = s;
}
}
class Blah extends Activity {
@Override
public void onCreate(Bundle b){
...
My*** ***State =((My***)get***licationContext());
String state = ***State.getState();
...
}
}
对于***lication的生命周期,今天测试了一下,***lication类型在该***被install的时候就已经实例化了,并且onCreate也已经被调用了。
如果需要创建类型里面可能需要用到的对象的话,就可以在构造函数里面实现,但是如果需要将该类型bind Service或者registerReceiver等操作时,需要将这些操作放到onCreate中,否则会抛出异常。其原因主要是这个对象还没有创建完成,此时***用这个对象来bindservice必然会出现意想不到的情况,所以在使用时还需要注意。
优点:
避免了饿汉式的那种在没有用到的情况下创建事例,**利用率高,不执行getInstance()就不会被实例,可以执行该类的其他静态方***。
缺点:
懒汉式在单个线程中没有问题,但多个线程同事访问的时候就可能同时创建多个实例,而且这多个实例不是同一个对象,虽然后面创建的实例会覆盖先创建的实例,但是还是会存在拿到不同对象的情况。
到此,以上就是小编对于j*ascript没有链表的问题就介绍到这了,希望介绍关于j*ascript没有链表的1点解答对大家有用。
[免责声明]本文来源于网络,不代表本站立场,如转载内容涉及版权等问题,请联系邮箱:83115484#qq.com,#换成@即可,我们会予以删除相关文章,保证您的权利。 转载请注明出处:http://www.zzbaijie.cn/junshixinwen/42267.html