分页: 4/9 第一页 上页 1 2 3 4 5 6 7 8 9 下页 最后页 [ 显示模式: 摘要 | 列表 ]
android获取手机的imei信息:

TelephonyManager telephonyManager=(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
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
Tags:
获取已安装的程序的信息(名称、包名、图标等)

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;
}


获取未安装的APK文件的信息
Tags: ,
简单的小问题,如果列出了所有安装的程序,怎么样打开该程序呢?

要调用该程序,有两个必要因素:packageName, className
比如打开计算器程序:

Intent i = new Intent();
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)
}
Tags: ,
简单的代码,测试至少是可行
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{
  }  
}
Tags:
安装某个apk文件:

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);


注:如果文件不存在会提示解析出错

卸载某个程序:

Uri packageURI = Uri.parse("package:com.aslibra.test");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);


参考阅读:

Install and Uninstall Android applications with PackageInstaller,包含判断是否开启安装第三方程序的支持

另外还有 Android Intent的几种用法全面总结,必要的时候可以参考啦

Tags:
代码非原创,fix了bug,完善的还是需要再思量:
/**
* 通过拼接的方式构造请求内容,实现参数传输以及文件传输
* @param actionUrl
* @param params
* @param files
* @return
* @throws IOException
*/
public static String post(String actionUrl, Map<String, String> params,
    Map<String, File> files) throws IOException {

  String BOUNDARY = java.util.UUID.randomUUID().toString();
  String PREFIX = "--" , LINEND = "\r\n";
  String MULTIPART_FROM_DATA = "multipart/form-data";
  String CHARSET = "UTF-8";

  URL uri = new URL(actionUrl);
  HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
  conn.setReadTimeout(5 * 1000); // 缓存的最长时间
  conn.setDoInput(true);// 允许输入
  conn.setDoOutput(true);// 允许输出
  conn.setUseCaches(false); // 不允许使用缓存
  conn.setRequestMethod("POST");
  conn.setRequestProperty("connection", "keep-alive");
  conn.setRequestProperty("Charsert", "UTF-8");
  conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);

  // 首先组拼文本类型的参数
  StringBuilder sb = new StringBuilder();
  for (Map.Entry<String, String> entry : params.entrySet()) {
    sb.append(PREFIX);
    sb.append(BOUNDARY);
    sb.append(LINEND);
    sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
    sb.append("Content-Type: text/plain; charset=" + CHARSET+LINEND);
    sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
    sb.append(LINEND);
    sb.append(entry.getValue());
    sb.append(LINEND);
  }

  DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
  outStream.write(sb.toString().getBytes());
  // 发送文件数据
  if(files!=null){
    int i = 0;
    for (Map.Entry<String, File> file: files.entrySet()) {
      StringBuilder sb1 = new StringBuilder();
      sb1.append(PREFIX);
      sb1.append(BOUNDARY);
      sb1.append(LINEND);
      sb1.append("Content-Disposition: form-data; name=\"file"+(i++)+"\"; filename=\""+file.getKey()+"\""+LINEND);
      sb1.append("Content-Type: application/octet-stream; charset="+CHARSET+LINEND);
      sb1.append(LINEND);
      outStream.write(sb1.toString().getBytes());

      InputStream is = new FileInputStream(file.getValue());
      byte[] buffer = new byte[1024];
      int len = 0;
      while ((len = is.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
      }

      is.close();
      outStream.write(LINEND.getBytes());
    }
  }
  
  //请求结束标志
  byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
  outStream.write(end_data);
  outStream.flush();

  //得到响应码
  int res = conn.getResponseCode();
  InputStream in = null;
  if (res == 200) {
    in = conn.getInputStream();
    int ch;
    StringBuilder sb2 = new StringBuilder();
    while ((ch = in.read()) != -1) {
      sb2.append((char) ch);
    }
  }
  return in == null ? null : in.toString();
}
Tags: ,
android的ImageSwitcher是一个图片更换而产生动画过渡的控件,appdemo里面有例子,其实是一个很好的图片浏览的例子,但有一个不好的地方就是选择小图时,速度太快,每个经过中点的小图都产生一次事件,导致大图切换效果不理想。
比如点击第一张,放大第一张,点击第三张,是先放大第二张,再放大第三张,滑动时经过的所有图片都显示一次,很不理想。
我们可以用线程来处理这个问题,一定的时间后如果选择的index值不变,说明已经稳定不变,显示大图。

修改一下即可:

/*
* 原代码
*/
//    public void onItemSelected(AdapterView parent, View v, int position, long id) {
//      mSwitcher.setImageResource(mImageIds[position]);
//    }

/*
* code: hqlulu
* http://www.aslibra.com
* 增加以下代码
*/
private int showingIndex = -1;
private static final int TIME_OUT_DISPLAY = 300;
private int toShowIndex = 0;

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  Log.v("test", "onItemSelected arg2="+arg2+" arg3="+arg3);

  toShowIndex = arg2;
  final Handler handler = new Handler() {  
    public void handleMessage(Message msg) {
      if(showingIndex != toShowIndex){
        showingIndex = toShowIndex;
        mSwitcher.setImageResource(mImageIds[toShowIndex]);          
      }
    }  
  };
  Thread checkChange = new Thread() {    
    public void run() {
      int myIndex = toShowIndex;
      try {
        sleep( TIME_OUT_DISPLAY );
        if( myIndex == toShowIndex ){
          handler.sendEmptyMessage(0);  
          Log.v("test", "+++ stable can show ...");
        }else{
          Log.v("test", "+++ not stable...");
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }  
  };
  checkChange.start();
}


完整代码见附件
Tags:
分页: 4/9 第一页 上页 1 2 3 4 5 6 7 8 9 下页 最后页 [ 显示模式: 摘要 | 列表 ]

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐