当前位置: 编程技术>移动开发
本页文章导读:
▪源码事例 源码例子
(补ant包)文件管理器源码,文件拖曳,listView弹性,root ,zip压缩/解
[Android实例]
[分享]Android四种播放器源码!
Android-opencv之CVCamera
......
▪ 第三章:手机的IO操作(剔除,改名字,打开) 第三章:手机的IO操作(删除,改名字,打开)
效果:main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:backgr.........
▪ 怎么设置3g网络-APN设置 如何设置3g网络-APN设置
一般用android系统的时候,我们使用wifi上网,但有时候我们也可以用3g上网,这里就需要设置一下3G接入点。具体设置主要是通过改变数据库数据来连接3g网络。
这里涉.........
[1]源码事例
来源: 互联网 发布时间: 2014-02-18
源码例子
[Android实例] [分享]Android四种播放器源码!
(补ant包)文件管理器源码,文件拖曳,listView弹性,root ,zip压缩/解
[Android实例] [分享]Android四种播放器源码!
Android-opencv之CVCamera
[2] 第三章:手机的IO操作(剔除,改名字,打开)
来源: 互联网 发布时间: 2014-02-18
第三章:手机的IO操作(删除,改名字,打开)
效果:
main.xml
file_row.xml
alert_dialog.xml
效果:
main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFF" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="2px" android:layout_y="6px" > </TextView> <ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView> </AbsoluteLayout>
file_row.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ImageView> <TextView android:id="@+id/text" android:layout_width="220px" android:layout_height="wrap_content" > </TextView> </LinearLayout>
alert_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <EditText android:id="@+id/editText" android:layout_width="138px" android:layout_height="wrap_content" android:textSize="18sp" android:layout_x="79px" android:layout_y="116px" > </EditText> </AbsoluteLayout>
package i.o.explorer.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class IOExplorerTest extends ListActivity {
/**
* Items 存放显示的名称
* paths 存放文件路径
* rootPath 起始目录
* */
private List<String> items=null;
private List<String> paths=null;
private String rootPaths="/";
private TextView mPath;
private View view;
private EditText myEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** 载入main.xml*/
setContentView(R.layout.main);
/** 通过id找到TextView组件*/
mPath=(TextView)findViewById(R.id.text);
getFileDir(rootPaths);
}
/** 取得文件的方法*/
private void getFileDir(String filePath){
/** 设置目前的目录*/
mPath.setText(filePath);
items=new ArrayList<String>();
paths=new ArrayList<String>();
File f=new File(filePath);
File[] files=f.listFiles();
if(!filePath.equals(rootPaths)){
/** 回到根目录*/
items.add("b1");
paths.add(rootPaths);
/** 回到上一层*/
items.add("b2");
paths.add(f.getParent());
}
if(files!=null){
/** 将文件添加到list当中*/
for(int i=0;i<files.length;i++){
File file=files[i];
items.add(file.getName());
paths.add(file.getPath());
}
}
setListAdapter(new MyAdapter(this,items,paths));
}
/** 被单击时所做的动作*/
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file=new File(paths.get(position));
if(file.isDirectory()){
/** 如果是文件就调用取得文件的方法*/
getFileDir(paths.get(position));
}else{
/** 否则就运行文件*/
fileHandle(file);
}
}
/** 处理文件的方法*/
private void fileHandle(final File file){
/** 单击文件是的点击事件*/
OnClickListener listener1=new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which==0){
/** 选择的item为打开的文件*/
openFile(file);
}else if(which==1){
/** 为选择的文件改名*/
LayoutInflater factory=LayoutInflater.from(IOExplorerTest.this);
/** 初始化对象,使用alert_dialog布局*/
view=factory.inflate(R.layout.alert_dialog, null);
/** 通过view找到EditText组件*/
myEditText=(EditText)view.findViewById(R.id.editText);
/** 设置EditText内容为点击文件的文件名*/
myEditText.setText(file.getName());
/** 实例一个更改文件的Dialog的确定的按钮的点击事件*/
OnClickListener listener2=new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/**获得修改后的文件路径*/
String modName=myEditText.getText().toString();
final String pFile=file.getParentFile().getPath()+"/";
final String newPath=pFile+modName;
/**判断文件名是否已存在*/
if(new File(newPath).exists()){
/**判断文件没修改就发送的状况*/
if(!modName.equals(file.getName())){
/**设置 AlertDialog的标题,消息,和按钮的点击事件*/
new AlertDialog.Builder(IOExplorerTest.this)
.setTitle("注意")
.setMessage("文件名已经存在!是否覆盖?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/**覆盖已存在的文件夹*/
file.renameTo(new File(newPath));
/**重新获得文件列表*/
getFileDir(pFile);
}
}).setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}else{
/**文件名不存在,直接修改文件名*/
file.renameTo(new File(newPath));
/**重新获得路径*/
getFileDir(pFile);
}
}
};
/**设置弹出的AlertDialog*/
AlertDialog renameDialog=new AlertDialog.Builder(IOExplorerTest.this).create();
renameDialog.setView(view);
/**设置删除的文件事件*/
renameDialog.setButton("确定", listener2);
renameDialog.setButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
renameDialog.show();
}else{
/**选择item为删除文件*/
new AlertDialog.Builder(IOExplorerTest.this).setTitle("注意!")
.setMessage("确定要删除文件吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/**删除文件*/
file.delete();
getFileDir(file.getParent());
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}
};
/**在手机上打开文件的提示信息*/
String[] menu={"打开文件夹","更改文件名","删除文件"};
new AlertDialog.Builder(IOExplorerTest.this)
.setTitle("你要做什么?")
.setItems(menu, listener1)
.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
/** 在手机上打开文件的方法*/
private void openFile(File f){
Intent intent=new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
/** 调用getMIMEType()的方法取得文件类型*/
String type=getMIMEType(f);
/** 设置intent的file与MimeType*/
intent.setDataAndType(Uri.fromFile(f), type);
startActivity(intent);
}
/** 判断文件类型的方法*/
private String getMIMEType(File f){
String type="";
String fName=f.getName();
/** 取得扩展名*/
String end=fName.substring(fName.lastIndexOf(".")+1,fName.length()).toLowerCase();
if(end.equals("m4a")||end.equals("mp3")||end.equals("mid")||end.equals("xmf")||end.equals("ogg")||end.equals("wav")){
type="audio";
}else if(end.equals("3gp")||end.equals("mp4")){
type="video";
}else if(end.equals("jpg")||end.equals("gif")||end.equals("png")||end.equals("jpeg")||end.equals("bmp")){
type="image";
}else{
type="*";
}
type+="/*";
return type;
}
}
package i.o.explorer.test;
import java.io.File;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
/**
* mIcon1回到根目录的图片
* mIcon2回到上一级图片
* mIcon3文件夹图片
* mIcon4文件图片
* */
private Bitmap mIcon1;
private Bitmap mIcon2;
private Bitmap mIcon3;
private Bitmap mIcon4;
private List<String> items;
private List<String> paths;
/**
* MyAdapter构造器传入三个参数
* */
public MyAdapter(Context c,List<String> lt,List<String> pa){
mInflater=LayoutInflater.from(c);
items=lt;
paths=pa;
mIcon1=BitmapFactory.decodeResource(c.getResources(),R.drawable.folder);
mIcon2=BitmapFactory.decodeResource(c.getResources(),R.drawable.folder);
mIcon3=BitmapFactory.decodeResource(c.getResources(),R.drawable.folder2);
mIcon4=BitmapFactory.decodeResource(c.getResources(),R.drawable.text);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
/**自定义layout作为loyout*/
convertView=mInflater.inflate(R.layout.file_row, null);
holder=new ViewHolder();
holder.text=(TextView) convertView.findViewById(R.id.text);
holder.icon=(ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
File f=new File(paths.get(position).toString());
if(items.get(position).toString().equals("b1")){
/**返回根目录*/
holder.text.setText("Back to..");
holder.icon.setImageBitmap(mIcon1);
}else if(items.get(position).toString().equals("b2")){
/**返回上一级*/
holder.text.setText("Back to..");
holder.icon.setImageBitmap(mIcon2);
}else{
holder.text.setText(f.getName());
if(f.isDirectory()){
/**设置文件或文件夹的图标*/
holder.icon.setImageBitmap(mIcon3);
}else{
holder.icon.setImageBitmap(mIcon4);
}
}
return convertView;
}
private class ViewHolder{
TextView text;
ImageView icon;
}
}
[3] 怎么设置3g网络-APN设置
来源: 互联网 发布时间: 2014-02-18
如何设置3g网络-APN设置
一般用android系统的时候,我们使用wifi上网,但有时候我们也可以用3g上网,这里就需要设置一下3G接入点。具体设置主要是通过改变数据库数据来连接3g网络。
这里涉及到两个URI,分别是apn列表uri:content://telephony/carriers,主apn的uri:content://telephony/carriers/preferapn,
首先,向apn列表中插入一行,需要的属性有name、apn和numeric。numeric要看不同的系统设置,我遇过的有的是46001,还有的是别的,没记得太清楚。然后根据插入的id,把该行设置为主apn的行。这样settings里面就会自动去读取。具体实现如下:
public void setApn(View v) {
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter.isEnabled()) {
adapter.disable();
} else {
setMainAPN(getAPNId());
}
}
private int getAPNId() {
int id = -1;
ContentValues values = new ContentValues();
values.put("name", "suking");
values.put("apn", "3gnet");
values.put("numeric", "46001");
ContentResolver resolver = getContentResolver();
Cursor c = null;
Uri newRow = resolver.insert(APN_URI, values);
if (newRow != null) {
c = resolver.query(newRow, null, null, null, null);
int idIndex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idIndex);
}
if (c != null) {
c.close();
}
return id;
}
private void setMainAPN(int id) {
ContentResolver resolver = getContentResolver();
ContentValues values = new ContentValues();
values.put("apn_id", id);
resolver.update(MAIN_APN, values, null, null);
}
注意到,在设置apn网络之前,首先是判断蓝牙适配器是否可用,如果可用需要把蓝牙关闭。
最新技术文章: