安卓怎么写文件( 二 )


“办公套件”能打开和编辑doc、xls、ppt、txt文档,且功能比较全,xls文档中粘贴函数,合并单元格都可以 。它还能以上几种格式文件互相另存为,另存为txt时还有非常全的编码供你选择 。但办公套件有7MB多,运行速度相对较慢 。
4. android 怎么写文件日志到SD卡上 android 如何写文件日志到SD卡上..
/**
* 写文件到sd卡上
*
* @param context
*/
public void writeFileToSD(String context) {
//使用RandomAccessFile 写文件 还是蛮好用的..推荐给大家使用 。
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
Log.d("TestFile", "SD card is not avaiable/writeable right now.");
return;
}
try {
String pathName = "/sdcard/";
String fileName = "log.txt";
File path = new File(pathName);
File file = new File(pathName + fileName);
if (!path.exists()) {
Log.d("TestFile", "Create the path:" + pathName);
path.mkdir();
}
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + fileName);
file.createNewFile();
}
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(file.length());
raf.write(context.getBytes());
raf.close();
//注释的也是写文件..但是每次写入都会把之前的覆盖..
/*String pathName = "/sdcard/";
String fileName = "log.txt";
File path = new File(pathName);
File file = new File(pathName + fileName);
if (!path.exists()) {
Log.d("TestFile", "Create the path:" + pathName);
path.mkdir();
}
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + fileName);
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(file);
String s = context;
byte[] buf = s.getBytes();
stream.write(buf);
stream.close();*/
} catch (Exception e) {
Log.e("TestFile", "Error on writeFilToSD.");
}
}
5. android怎么写aidl文件 建立AIDL服务要比建立普通的服务复杂一些,具体步骤如下:(1)在Eclipse Android工程的Java包目录中建立一个扩展名为aidl的文件 。
该文件的语法类似于Java代码,但会稍有不同 。详细介绍见实例52的内容 。
(2)如果aidl文件的内容是正确的,ADT会自动生成一个Java接口文件(*.java) 。(3)建立一个服务类(Service的子类) 。
(4)实现由aidl文件生成的Java接口 。(5)在AndroidManifest.xml文件中配置AIDL服务,尤其要注意的是,标签中android:name的属性值就是客户端要引用该服务的ID,也就是Intent类的参数值 。
建立AIDL服务本例中将建立一个简单的AIDL服务 。这个AIDL服务只有一个getValue方法,该方法返回一个String类型的值 。
在安装完服务后,会在客户端调用这个getValue方法,并将返回值在TextView组件中输出 。建立这个AIDL服务的步骤如下:(1)建立一个aidl文件 。
在Java包目录中建立一个IMyService.aidl文件 。IMyService.aidl文件的位置如图IMyService.aidl文件的内容如下:Java代码:package eoe.demo;interface IMyService { String getValue(); }IMyService.aidl文件的内容与Java代码非常相似,但要注意,不能加修饰符(例如,public、private)、AIDL服务不支持的数据类型(例如,InputStream、OutputStream)等内容 。
(2)如果IMyService.aidl文件中的内容输入正确,ADT会自动生成一个IMyService.java文件 。读者一般并不需要关心这个文件的具体内容,也不需要维护这个文件 。
关于该文件的具体内容,读者可以查看本节提供的源代码 。(3)编写一个MyService类 。
MyService是Service的子类,在MyService类中定义了一个内嵌类(MyServiceImpl),该类是IMyService.Stub的子类 。MyService类的代码如下:Java代码:package eoe.demo;import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException;public class MyService extends Service { public class MyServiceImpl extends IMyService.Stub { @Override public String getValue() throws RemoteException { return "Android/OPhone开发讲义"; } } @Override public IBinder onBind(Intent intent) { return new MyServiceImpl(); }}在编写上面代码时要注意如下两点:IMyService.Stub是根据IMyService.aidl文件自动生成的,一般并不需要管这个类的内容,只需要编写一个继承于IMyService.Stub类的子类(MyServiceImpl类)即可 。