手机开发的相关内容,也许以后会有收集 (Android etc)
读取文件列表时,获取到文件的修改时间,但怎么转换为正常可辨的时间值呢?
把大小转换为易读的格式,这里偷懒,最大是M的:
//function A
//CODE...
File myfile = new File(apk);
long mytime = myfile.lastModified();
String timeStr = time2String( mytime );
//end of A
private String time2String( long time ){
//方式1 按系统的日期方式
// Calendar cal = Calendar.getInstance();
// cal.setTimeInMillis( time );
// return cal.getTime().toLocaleString();
//方式2 按自定义格式
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Date currentTime = new Date( time );
return format1.format(currentTime);
}
//CODE...
File myfile = new File(apk);
long mytime = myfile.lastModified();
String timeStr = time2String( mytime );
//end of A
private String time2String( long time ){
//方式1 按系统的日期方式
// Calendar cal = Calendar.getInstance();
// cal.setTimeInMillis( time );
// return cal.getTime().toLocaleString();
//方式2 按自定义格式
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Date currentTime = new Date( time );
return format1.format(currentTime);
}
把大小转换为易读的格式,这里偷懒,最大是M的:
前面有说明android获取程序信息,这些信息是基本信息,我想提取android:minSdkVersion的值,可还没有发现用程序可以提取出来。
有一个比较笨的方式是解压apk文件,分析apk文件里的xml。
首先apk文件其实是zip文件,改名即可按zip文件解压,而xml文件是二进制的,倒是需要另外的软件解决。
如果需要提取,不需要再重新打包,可以改名直接解压,否则可以试试apktool来解压和打包,不重复说明。
浏览二进制的xml文件,可以用 AXMLPrinter2 解决,在 android4me 的工程里面可以下载到。
AXMLPrinter2.jar Prints XML document from binary XML file (with correct namespace handing & attribute formatting).
下面演示一下解析过程:
有一个比较笨的方式是解压apk文件,分析apk文件里的xml。
首先apk文件其实是zip文件,改名即可按zip文件解压,而xml文件是二进制的,倒是需要另外的软件解决。
如果需要提取,不需要再重新打包,可以改名直接解压,否则可以试试apktool来解压和打包,不重复说明。
浏览二进制的xml文件,可以用 AXMLPrinter2 解决,在 android4me 的工程里面可以下载到。
AXMLPrinter2.jar Prints XML document from binary XML file (with correct namespace handing & attribute formatting).
下面演示一下解析过程:
root@aslibra:/tmp/apk# ls
c.apk
root@aslibra:/tmp/apk# mv c.apk c.zip
root@aslibra:/tmp/apk# unzip c
Archive: c.zip
...
inflating: res/layout/main.xml
...
inflating: AndroidManifest.xml
extracting: resources.arsc
...
root@aslibra:/tmp/apk# java -jar /path/to/AXMLPrinter2.jar AndroidManifest.xml |grep minSdkVersion
android:minSdkVersion="2"
c.apk
root@aslibra:/tmp/apk# mv c.apk c.zip
root@aslibra:/tmp/apk# unzip c
Archive: c.zip
...
inflating: res/layout/main.xml
...
inflating: AndroidManifest.xml
extracting: resources.arsc
...
root@aslibra:/tmp/apk# java -jar /path/to/AXMLPrinter2.jar AndroidManifest.xml |grep minSdkVersion
android:minSdkVersion="2"
android获取手机的imei信息:
这个操作需要权限说明:
另外,要获得手机的型号和系统版本,可以这样:
TelephonyManager telephonyManager=(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String imei=telephonyManager.getDeviceId();
//获得手机号
telephonyManager.getLine1Number();
String imei=telephonyManager.getDeviceId();
//获得手机号
telephonyManager.getLine1Number();
这个操作需要权限说明:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
另外,要获得手机的型号和系统版本,可以这样:
//机器型号 HTC Magic
Build.MODEL
//SDK版本 8
Build.VERSION.SDK
//SDK版本号 2.2
Build.VERSION.RELEASE
Build.MODEL
//SDK版本 8
Build.VERSION.SDK
//SDK版本号 2.2
Build.VERSION.RELEASE
获取已安装的程序的信息(名称、包名、图标等)
获取未安装的APK文件的信息
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
log(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode + "\t");
}
}
private void listPackages() {
ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
final int max = apps.size();
for (int i=0; i<max; i++) {
apps.get(i).prettyPrint();
}
}
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
log(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode + "\t");
}
}
private void listPackages() {
ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
final int max = apps.size();
for (int i=0; i<max; i++) {
apps.get(i).prettyPrint();
}
}
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
获取未安装的APK文件的信息
简单的小问题,如果列出了所有安装的程序,怎么样打开该程序呢?
要调用该程序,有两个必要因素:packageName, className
比如打开计算器程序:
前面是packageName,后面是className,我们尝试取出此两个值即可。
要调用该程序,有两个必要因素:packageName, className
比如打开计算器程序:
Intent i = new Intent();
i.setClassName("com.android.calculator2", "com.android.calculator2.Calculator");
startActivity(i);
i.setClassName("com.android.calculator2", "com.android.calculator2.Calculator");
startActivity(i);
前面是packageName,后面是className,我们尝试取出此两个值即可。
List<PackageInfo> packs = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
//p.packageName;
//p.activities[0].name; //(className)
}
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
//p.packageName;
//p.activities[0].name; //(className)
}
简单的代码,测试至少是可行
public static void downloadFile(String url, String newPath) {
URL myFileUrl = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
Log.e("test", "MalformedURLException: "+e.toString() );
}
try {
HttpURLConnection conn;
conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
FileOutputStream newfile = new FileOutputStream("/sdcard/tmp.apk");
byte[] buffer = new byte[1444];
int byteread = 0;
while ( (byteread = is.read(buffer)) != -1) {
newfile.write(buffer, 0, byteread);
}
is.close();
} catch (Exception e) {
Log.e("test", "returnNetworkBitMap: "+e.toString() );
}finally{
}
}
URL myFileUrl = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
Log.e("test", "MalformedURLException: "+e.toString() );
}
try {
HttpURLConnection conn;
conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
FileOutputStream newfile = new FileOutputStream("/sdcard/tmp.apk");
byte[] buffer = new byte[1444];
int byteread = 0;
while ( (byteread = is.read(buffer)) != -1) {
newfile.write(buffer, 0, byteread);
}
is.close();
} catch (Exception e) {
Log.e("test", "returnNetworkBitMap: "+e.toString() );
}finally{
}
}
安装某个apk文件:
注:如果文件不存在会提示解析出错
卸载某个程序:
参考阅读:
Install and Uninstall Android applications with PackageInstaller,包含判断是否开启安装第三方程序的支持
另外还有 Android Intent的几种用法全面总结,必要的时候可以参考啦
String fileName = "/sdcard/tmp.apk";
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(fileName) ), "application/vnd.android.package-archive");
startActivity(i);
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(fileName) ), "application/vnd.android.package-archive");
startActivity(i);
注:如果文件不存在会提示解析出错
卸载某个程序:
Uri packageURI = Uri.parse("package:com.aslibra.test");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
参考阅读:
Install and Uninstall Android applications with PackageInstaller,包含判断是否开启安装第三方程序的支持
另外还有 Android Intent的几种用法全面总结,必要的时候可以参考啦





