随笔-46  评论-31  文章-1  trackbacks-0
  2012年5月8日

linux下通过C执行命令的时候一半都是使用system()方法,但是该方法执行命令返回的值是-1或0,而有时候我们需要得到执行命令后的结果。可以使用管道实现

输出到文件流的函数是popen(),例如

FILE *isr;

isr = popen("ls -l","r"); ls -l命令的输出通过管道读取("r"参数)到isr

下面是演示例子,列出当前可用的loop设备,(必须是root权限才可以执行losetup -f)

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

char* cmd_system(const char* command);
int main()
{
    //char str[20]={"0"};
    char* result = cmd_system("losetup -f");
   
//通过该方法可以将char*转换为char数组 //strcpy(str,result); printf("The result:%s\n",result); return 0; } char* cmd_system(const char* command) { char* result = ""; FILE *fpRead; fpRead = popen(command, "r"); char buf[1024]; memset(buf,'\0',sizeof(buf)); while(fgets(buf,1024-1,fpRead)!=NULL) {
       result
= buf; } if(fpRead!=NULL) pclose(fpRead); return result; }

执行结果:

The result:/dev/loop0

posted @ 2012-05-08 21:21 娄立军 阅读(71) 评论(2) 编辑
  2012年4月25日

1、错误:Conversion to Dalvik format failed with error 1

控制台的错误就是上面的样子,代码中不会有错误,但是不能运行

解决方案:项目-->Properties-->Java Build Path-->Libraries-->删除android.jar即可(这个android.jar可能是你fix project properites时系统自动加入的)

 

2、错误:创建项目时弹出“找不到 \android-sdk-linux\tools\lib\proguard.cfg文件”的错误

原因:SDK不完整(比如你自己编译源码生成的SDK)

解决方案1:更新SDK

解决方案2:从其他人的android-sdk-linux\tools\lib\下拷贝proguard.cfg到你的这个目录下,或者自己建立一个,内容如下

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
posted @ 2012-04-25 21:46 娄立军 阅读(200) 评论(0) 编辑
  2012年4月23日

做其他开发时的一个小例子,主要的功能

PackageManager的功能:

  •安装,卸载应用
  •查询permission相关信息
  •查询Application相关信息(application,activity,receiver,service,provider及相应属性等)
  •查询已安装应用
  •增加,删除permission
  •清除用户数据、缓存,代码段等

先看看效果图,风格可以自己调整

代码就暂时不特别规范的写了,只是测试程序

main.xml,整体布局,默认TextView是没有事件监听的,需要设置其android:clickable属性为true

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_color"
    android:orientation="vertical" >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="10dip"
        />
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        >
        <TextView
            android:id="@+id/button01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="普通应用"
            android:gravity="center"
            android:background="@drawable/button_selector"
            android:focusable="true"
            android:clickable="true"
            android:layout_marginLeft="10dip"
            />
        <View android:layout_width="5px" android:layout_height="fill_parent"
            android:background="#FFFFFFFF"/>
        <TextView
            android:id="@+id/button02"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="系统应用"
            android:gravity="center"
            android:background="@drawable/button_selector"
            android:focusable="true"
            android:clickable="true"
            android:layout_marginRight="10dip"
            />
    </LinearLayout>
    <ListView 
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fastScrollEnabled="true">
        
    </ListView>"

</LinearLayout>

ListView的item布局:listitem.xml,这里ImageView的大小最好设置为固定的,如果是wrap_content的话,不同应用程序的图标不一样大,显示出来很难看

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_item_color_bg"
    android:orientation="horizontal" >

    <ImageView 
        android:id="@+id/appicon"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:padding="5dp"
        />
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <TextView 
            android:id="@+id/appName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/packageName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
</LinearLayout>

下面的是drawable下的一些文件

background_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:startColor="#FFFFFFFF"
        android:endColor="#FFFFFFFF"
        android:angle="270.0"
        android:centerY="0.3"
        android:centerColor="#FFBDBDBD"
        />

</shape>

button_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient 
        android:startColor="#FF7F7F7F"
        android:endColor="#FF000000"
        android:angle="270.0"
        />

</shape>

button_selector.xml,这是点击TextView自定义的Button的selector文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize="true" >

    <!-- 获得焦点时的背景图片 -->
    <item 
        android:state_focused="true"
        >
        <shape>
            <gradient 
                android:startColor="#FFE5CF33"
                android:endColor="#FFF1E7A2"
                android:angle="90.0"
                />
        </shape>
    </item>
    <!-- 设置响应所有事件 -->
    <item android:state_enabled="true"
        android:state_pressed="false"
        >
        <shape>
            <gradient 
                android:startColor="#FF1B1B1B"
                android:endColor="#FF969696"
                android:angle="90.0"
                />
        </shape>
    </item>
    <!-- 按钮点击时候的背景 -->
    <item android:state_enabled="true"
        android:state_pressed="true">
        <shape>
            <gradient 
                android:startColor="#FF000000"
                android:endColor="#FF474747"
                android:angle="90.0"
                />
        </shape>
    </item>
    <item android:state_enabled="false"
        android:state_pressed="true">
        <shape>
            <gradient 
                android:startColor="#FF000000"
                android:endColor="#ff474747"
                android:angle="90.0"
                />
        </shape>
    </item>
    <!-- 默认情况下的背景 -->
    <item>
        <shape>
            <gradient 
                android:startColor="#FF000000"
                android:endColor="#FF474747"
                android:angle="90.0"
                />
        </shape>
    </item>

</selector>

list_item_color_bg.xml,这是点击ListView时item自定义的选中背景

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:state_pressed="true"
        android:drawable="@color/green"
        ></item>
    <item 
        android:drawable="@color/white"
        ></item>

</selector>

最后是源代码了

这里有几点注意

package com.loulijun.appshow;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AppShowActivity extends Activity {
    /** Called when the activity is first created. */
    private TextView customAppsBtn, systemAppsBtn;
    private ListView lv;
    private List<PackageInfo> customApps; // 普通应用程序
    private List<PackageInfo> systemApps; // 系统应用程序
    MyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        customAppsBtn = (TextView)findViewById(R.id.button01);
        systemAppsBtn = (TextView)findViewById(R.id.button02);
        lv = (ListView)findViewById(R.id.lv);
        //加载系统应用和普通应用程序
        loadApps();
        adapter = new MyAdapter(this, customApps);
        lv.setAdapter(adapter);
        
        customAppsBtn.setOnClickListener(new TextView.OnClickListener()
        {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "普通应用", Toast.LENGTH_SHORT).show();
                //切换到普通程序列表
                updateAppList(customApps);
            }
            
        });
        systemAppsBtn.setOnClickListener(new TextView.OnClickListener()
        {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "系统应用", Toast.LENGTH_SHORT).show();
                //切换到系统程序列表
                updateAppList(systemApps);
            }
            
        });
    }
    
    //列出普通应用程序
    private void loadApps()
    {
        
        customApps = new ArrayList<PackageInfo>();
        systemApps = new ArrayList<PackageInfo>();
        //得到PackageManager对象
        PackageManager pm = this.getPackageManager();
        //得到系统安装的所有程序包的PackageInfo对象
        List<PackageInfo> packages = pm.getInstalledPackages(0);

        for(PackageInfo pi:packages)
        {
            //列出普通应用
            if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)<=0) 
            {
                customApps.add(pi);
            }
            //列出系统应用,总是感觉这里设计的有问题,希望高手指点
            if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)>0) 
            {
                systemApps.add(pi);
            }
        }
    }
  
    private void updateAppList(List<PackageInfo> apps)
    {
        adapter.setData(apps);
        adapter.notifyDataSetChanged();
    }
    
    //ViewHolder静态类
    static class ViewHolder
    {
        public ImageView appicon;
        public TextView appName;
        public TextView packageName;
    }
    
    class MyAdapter extends BaseAdapter
    {
        private LayoutInflater mInflater = null;
        private List<PackageInfo> apps;
        private MyAdapter(Context context, List<PackageInfo> apps)
        {
            this.mInflater = LayoutInflater.from(context);            
            this.apps = apps;
        }
        //setData()要在MyAdapter类里设置,用于设置数据源
        public void setData(List<PackageInfo> apps)
        {
            this.apps = apps;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return customApps.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return 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 = null;
            
            
            if(convertView == null)
            {
                holder = new ViewHolder();
    
                convertView = mInflater.inflate(R.layout.list_item, null);
                holder.appicon = (ImageView)convertView.findViewById(R.id.appicon);
                holder.appName = (TextView)convertView.findViewById(R.id.appName);
                holder.packageName = (TextView)convertView.findViewById(R.id.packageName);
                convertView.setTag(holder);
            }else
            {
                holder = (ViewHolder)convertView.getTag();
            }
            PackageManager pm = getPackageManager(); // 得到pm对象
            PackageInfo info = apps.get(position);
            ApplicationInfo appInfo = info.applicationInfo;
            
            holder.appicon.setImageDrawable(pm.getApplicationIcon(appInfo));
            holder.appName.setText(pm.getApplicationLabel(appInfo));
            holder.packageName.setText(appInfo.packageName);
            return convertView;
        }
        
    }
}
posted @ 2012-04-23 21:30 娄立军 阅读(207) 评论(0) 编辑
  2012年4月16日

  最近遇到个问题。编译生成的ROM在之前还可以刷进去的,后来莫名其妙的就不能刷了,而且用以前可以刷进去的备份也是不能刷,但是官方的ROM却可以刷,这个倒是让人挺郁闷的

  刷机后停留在开机界面有如下几种原因(有对应的解决方案)

1、刷机时忘记了双wipe,也就是进入recovery后wipe data和wipe cache ,这个一般有点经验的都懂。如果你刷机时不wipe的话可能就会停留在开机界面

2、如果上面两步wipe data和wipe cache后刷机还是这样,可以在双wipe后再mounts and storage-->format/system,因为wipe只是清空用户数据回到原始状态,而format是彻底删除

3、如果你遇到卡在开机界面的情况,可能会拔电池,我拔过n回。这样就可能造成recovery的损坏,所以你可以通过Rom Manager重新刷人recovery

4、有时候recovery版本过高也是不能刷进去的,这个时候可以选择降级试试,也就是刷入低版本的recovery

5、内核不匹配,你需要下载与你手机及系统相匹配的内核刷进去即可

6、bootloader损坏,这个倒是没遇到过,不过如果真是它的问题,也可以从新刷入匹配的bootloader来解决

 

这里还要注意:

1、fastboot如果刷recovery.img或者boot.img时如果是驱动没有安装好,会出现waiting for devices的错误(这个可以用豌豆荚安装驱动就行)

2、如果你的手机还是S-ON的话,需要刷成S-OFF,否则就会出现这个not allowed的错误

posted @ 2012-04-16 16:14 娄立军 阅读(233) 评论(0) 编辑
  2012年4月15日

在写文件管理系统时会用到各种打开不同格式的文件的需求,由于Android系统默认内置了一些可以打开的系统应用,但还是不能满足需求,比如打开视频文件、word等,需要安装相应的播放软件才可以使用,这时程序会通过Intent查找可以使用的软件

实现通过代码打开一个文件需要2部分,一部分是要获取到不同文件的后缀,以便根据需求匹配相应的Intent,另一个就是不同格式的文件打开的Intent不同

1、在values目录下定义后缀数组文件fileendings

<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="fileEndingImage">
        <item>.png</item>
        <item>.gif</item>
        <item>.jpg</item>
        <item>.jpeg</item>
        <item>.bmp</item>
    </array>
<array name="fileEndingAudio">
        <item>.mp3</item>
        <item>.wav</item>
        <item>.ogg</item>
        <item>.midi</item>
    </array>
<array name="fileEndingVideo">
        <item>.mp4</item>
        <item>.rmvb</item>
        <item>.avi</item>
        <item>.flv</item>
    </array>
<array name="fileEndingPackage">
        <item>.jar</item>
        <item>.zip</item>
        <item>.rar</item>
        <item>.gz</item>
        <item>.apk</item>
        <item>.img</item>
    </array>
<array name="fileEndingWebText">
        <item>.htm</item>
        <item>.html</item>
        <item>.php</item>
        <item>.jsp</item>
    </array>
<array name="fileEndingText">
        <item>.txt</item>
        <item>.java</item>
        <item>.c</item>
        <item>.cpp</item>
        <item>.py</item>
        <item>.xml</item>
        <item>.json</item>
        <item>.log</item>
    </array>
<array name="fileEndingWord">
    <item>.doc</item>
    <item>.docx</item>
</array>
<array name="fileEndingExcel">
    <item>.xls</item>
    <item>.xlsx</item>
</array>
<array name="fileEndingPPT">
    <item>.ppt</item>
    <item>.pptx</item>
</array>

<array name="fileEndingPdf">
    <item>.pdf</item>
</array>
</resources>

2、定义OpenFiles工具类,只需传输File参数即可,然后通过返回的Intent打开文件

public class OpenFiles {
     //android获取一个用于打开HTML文件的intent
       public static Intent getHtmlFileIntent(File file)
       {
           Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();
           Intent intent = new Intent("android.intent.action.VIEW");
           intent.setDataAndType(uri, "text/html");
           return intent;
       }
     //android获取一个用于打开图片文件的intent
       public static Intent getImageFileIntent(File file)
       {
           Intent intent = new Intent("android.intent.action.VIEW");
           intent.addCategory("android.intent.category.DEFAULT");
           intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           Uri uri = Uri.fromFile(file);
           intent.setDataAndType(uri, "image/*");
           return intent;
       }
       //android获取一个用于打开PDF文件的intent
       public static Intent getPdfFileIntent(File file)
       {
         Intent intent = new Intent("android.intent.action.VIEW");
         intent.addCategory("android.intent.category.DEFAULT");
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "application/pdf");
         return intent;
       }
     //android获取一个用于打开文本文件的intent
     public static Intent getTextFileIntent(File file)
     {    
       Intent intent = new Intent("android.intent.action.VIEW");
       intent.addCategory("android.intent.category.DEFAULT");
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       Uri uri = Uri.fromFile(file);
       intent.setDataAndType(uri, "text/plain");
       return intent;
     }
    
     //android获取一个用于打开音频文件的intent
       public static Intent getAudioFileIntent(File file)
       {
         Intent intent = new Intent("android.intent.action.VIEW");
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         intent.putExtra("oneshot", 0);
         intent.putExtra("configchange", 0);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "audio/*");
         return intent;
       }
       //android获取一个用于打开视频文件的intent
       public static Intent getVideoFileIntent(File file)
       {
         Intent intent = new Intent("android.intent.action.VIEW");
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         intent.putExtra("oneshot", 0);
         intent.putExtra("configchange", 0);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "video/*");
         return intent;
       }
    
    
       //android获取一个用于打开CHM文件的intent
       public static Intent getChmFileIntent(File file)
       {
         Intent intent = new Intent("android.intent.action.VIEW");
         intent.addCategory("android.intent.category.DEFAULT");
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "application/x-chm");
         return intent;
       }
    
    
     //android获取一个用于打开Word文件的intent
       public static Intent getWordFileIntent(File file)
       {
         Intent intent = new Intent("android.intent.action.VIEW");
         intent.addCategory("android.intent.category.DEFAULT");
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "application/msword");
         return intent;
       }
     //android获取一个用于打开Excel文件的intent
       public static Intent getExcelFileIntent(File file)
       {
         Intent intent = new Intent("android.intent.action.VIEW");
         intent.addCategory("android.intent.category.DEFAULT");
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "application/vnd.ms-excel");
         return intent;
       }
     //android获取一个用于打开PPT文件的intent
       public static Intent getPPTFileIntent(File file)
       {
         Intent intent = new Intent("android.intent.action.VIEW");
         intent.addCategory("android.intent.category.DEFAULT");
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
         return intent;
       }
       //android获取一个用于打开apk文件的intent
       public static Intent getApkFileIntent(File file)
       {
           Intent intent = new Intent();  
           intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
           intent.setAction(android.content.Intent.ACTION_VIEW);  
           intent.setDataAndType(Uri.fromFile(file),  "application/vnd.android.package-archive");  
           return intent;
       }
       
       
}

 

3、定义用于检查要打开的文件的后缀是否在遍历后缀数组中

    private boolean checkEndsWithInStringArray(String checkItsEnd, 
                    String[] fileEndings){
        for(String aEnd : fileEndings){
            if(checkItsEnd.endsWith(aEnd))
                return true;
        }
        return false;
    }

4、通过调用OpenFiles类返回的Intent,打开相应的文件

if(currentPath!=null&&currentPath.isFile())
                {
                    String fileName = currentPath.toString();
                    Intent intent;
                    if(checkEndsWithInStringArray(fileName, getResources().
                            getStringArray(R.array.fileEndingImage))){
                        intent = OpenFiles.getImageFileIntent(currentPath);
                        startActivity(intent);
                    }else if(checkEndsWithInStringArray(fileName, getResources().
                            getStringArray(R.array.fileEndingWebText))){
                        intent = OpenFiles.getHtmlFileIntent(currentPath);
                        startActivity(intent);
                    }else if(checkEndsWithInStringArray(fileName, getResources().
                            getStringArray(R.array.fileEndingPackage))){
                        intent = OpenFiles.getApkFileIntent(currentPath);
                        startActivity(intent);

                    }else if(checkEndsWithInStringArray(fileName, getResources().
                            getStringArray(R.array.fileEndingAudio))){
                        intent = OpenFiles.getAudioFileIntent(currentPath);
                        startActivity(intent);
                    }else if(checkEndsWithInStringArray(fileName, getResources().
                            getStringArray(R.array.fileEndingVideo))){
                        intent = OpenFiles.getVideoFileIntent(currentPath);
                        startActivity(intent);
                    }else if(checkEndsWithInStringArray(fileName, getResources().
                            getStringArray(R.array.fileEndingText))){
                        intent = OpenFiles.getTextFileIntent(currentPath);
                        startActivity(intent);
                    }else if(checkEndsWithInStringArray(fileName, getResources().
                            getStringArray(R.array.fileEndingPdf))){
                        intent = OpenFiles.getPdfFileIntent(currentPath);
                        startActivity(intent);
                    }else if(checkEndsWithInStringArray(fileName, getResources().
                            getStringArray(R.array.fileEndingWord))){
                        intent = OpenFiles.getWordFileIntent(currentPath);
                        startActivity(intent);
                    }else if(checkEndsWithInStringArray(fileName, getResources().
                            getStringArray(R.array.fileEndingExcel))){
                        intent = OpenFiles.getExcelFileIntent(currentPath);
                        startActivity(intent);
                    }else if(checkEndsWithInStringArray(fileName, getResources().
                            getStringArray(R.array.fileEndingPPT))){
                        intent = OpenFiles.getPPTFileIntent(currentPath);
                        startActivity(intent);
                    }else
                    {
                        showMessage("无法打开,请安装相应的软件!");
                    }
                }else
                {
                    showMessage("对不起,这不是文件!");
                }

效果

posted @ 2012-04-15 22:16 娄立军 阅读(385) 评论(1) 编辑

默认情况下使用ListView背景色是黑色,选中item的高亮颜色是菊黄色,很多时候不得不自己定义背景色或者背景图

android:cacheColorHint="@android:color/transparent",意思为去黑色底色,比如ListView滚动时会刷新界面,默认颜色还是系统颜色,所以采用这种方式设置其为透明即可,这个属性在ListView中使用圆角图片来设置ListView时很有用

android:divider="@null"用于去掉listview的item之间的黑线

1、背景色

即在list_item_color_bg.xml中通过设置color来实现点击item时不同的颜色,但是如果使用color的话,listview无法使用android:listSelector属性,如果设置android:listSelector方式的话,点击一个item后整体的ListView全部都会变成一种颜色,这时必须采用在item中设置android:background的方式才可以。android:listSelector方式适用于图片的方式,即类似与(android:drawable="@drawable/img")

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/green"></item>
    <item android:drawable="@color/white"></item>
</selector>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffff</color>
    <color name="black">#000000</color>
    <color name="green">#00ff00</color>
</resources>

下面再看看布局文件

listview.xml,用color的方式,这里不能使用listSelector

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView 
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fastScrollEnabled="true"
        android:cacheColorHint="@android:color/transparent"
        android:divider="@null"
        />
</LinearLayout>

list_item_color.xml,通过color设置直接在item的布局中设置背景即可

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/list_item_color_bg">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
        />
        <TextView 
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            />
    </LinearLayout>
</LinearLayout>

效果图

2、背景图

这种方式是在selector文件中采用图片来设置item的背景,无论是设置ListView的android:listSelector的方式还是设置item的android:background的方式都可以使用,不过最好还是使用android:background的方式,因为使用android:listSelector的方式时下面的selector文件中设置的默认时的图片

<item android:drawable="@drawable/login_input"/>)不会显示,而改为background的方式则可以。有些奇怪,希望懂的能指点一下

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/input_over"/>
    <item android:drawable="@drawable/login_input"/>
</selector>

listView此时设置如下,这里在item中不设置android:background

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView 
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fastScrollEnabled="true"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@drawable/list_item_drawable_bg"
        />
</LinearLayout>

此时的效果图如下:背景图是.9.png图片,注意默认的白色.9.png图片login_input没有显示

如果使用android:background的方式,取消android:listSelector的方式,效果如下

posted @ 2012-04-15 16:03 娄立军 阅读(649) 评论(2) 编辑
  2012年4月11日
摘要: 客户端:1、登录时检查网络状态2、登录加载进度条3、登录服务器端进行验证,如果用户名和密码存在且正确,则登录,否则失败4、注册时将用户信息保存到服务器端数据库中(MySQL)5、记住密码功能(还不完善,只是测试)6、对密码信息进行md5()单向加密服务器端:1、接收客户端发来的登录请求,如果用户名和密码存在于MySQL数据库中则返回客户端一个响应信息"success"2、接收客户端发来的注册请求,将用户名和密码存放到MySQL数据库中不过目前还存在很多问题,以后有时间继续更新下面是效果图:完整代码下载:http://115.com/file/bexv3qlf#LoginDe阅读全文
posted @ 2012-04-11 16:29 娄立军 阅读(623) 评论(1) 编辑
  2012年4月10日
摘要: 引起Android内存泄露有很多种原因,下面罗列了一些问题,以后会一一解决1、构造Adapter时没有使用缓存convertView(衍生出ListView优化问题)2、查询数据库游标没有关闭3、Activity中生命周期对象大于Activity生命周期(关于Application Context与Activity Context)4、Bitmap对象不使用时没有recycle掉(这里还有其他解决方案)今天说的是第一种:如何使用缓存来优化ListView因为如果不使用缓存convertView的话,调用getView时每次都会重新创建View,这样之前的View可能还没有销毁,加之不断的新建V阅读全文
posted @ 2012-04-10 15:47 娄立军 阅读(376) 评论(1) 编辑
  2012年4月6日
摘要: 错误如下,不知道是不是遇到鬼了,之前好好的,装了myeclipse以后出错了(说实话,myeclipse用的很不爽,感觉netbeans更好用些)HTTP Status 500 -type Exception reportmessagedescription The server encountered an internal error () that prevented it from fulfilling this request.exceptionjavax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/阅读全文
posted @ 2012-04-06 11:15 娄立军 阅读(301) 评论(0) 编辑
  2012年4月1日
摘要: 一般如果需要将内网IP映射到外网首先需要确定你用的是那种类型的宽带,有LAN、ADSL等,如果是ADSL电话,由于每次获取的都是动态IP,这就需要通过动态域名绑定的方式来实现内网IP访问外网。NAT原理可参见:http://www.cnblogs.com/loulijun/archive/2012/03/31/2426621.html准备工作:1、下载花生壳软件2、保证你能够访问你的网关,需要修改路由配置详细步骤1、首先需要到http://www.oray.com/(花生壳官网)下载一个花生壳软件并安装,并且注册一个护照。2、由于ADSL的modem自带NAT穿透功能,所以只需要配置一下即可登阅读全文
posted @ 2012-04-01 11:23 娄立军 阅读(470) 评论(2) 编辑
仅列出标题  下一页