开发时的知识点记录

一些知识点:

  1. 通过号码查找联系人:PhoneLookup .CONTENT_FILTER_URI
  2. 关闭系统Dialog的广播:Intent.ACTION_CLOSE_SYSTEM_DIALOGS
  3. SpannableString  //设置String高亮
  4. 对于Button和ImageButton 还有一些View 设置半透明或者透明:
    <span style="font-size:14px;">android:background="#b0000000" //这是就是半透明
    android:background="#00000000" //就是全透明
    android:background="#FFFFFFFF" //就是不透明</span>


获取联系人头像:

ContentResolver cr = view.getContext().getContentResolver();
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
Long.parseLong(contact.contact_id));
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
Bitmap photo = BitmapFactory.decodeStream(input);
获取联系人的高清头像:
cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, Contacts._ID
          + " = ?", new String[] {
             personId
}, null);
if (cursor != null && cursor.moveToNext()) {
    String displayPhotoId = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_FILE_ID));
    AssetFileDescriptor afd = cr.openAssetFileDescriptor(Uri.parse("content://com.android.contacts/display_photo/"
                                        + displayPhotoId), "r");
    FileInputStream fis = afd.createInputStream();
    Bitmap bitmap = BitmapFactory.decodeStream(fis);
}
删除联系人头像:
ContentValues values = new ContentValues();
Uri u = Uri.parse("content://com.android.contacts/data");
int photoRow = -1;
String where = "raw_contact_id = " + personId + " AND mimetype ='vnd.android.cursor.item/photo'";
Cursor cursor = cr.query(u, null, where, null, null);
if (cursor != null) {
    if (cursor.moveToFirst()) {
        photoRow = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
    }
    cursor.close();
}
values.put("raw_contact_id", personId);
values.put("is_super_primary", 1);
values.put("data15", "");
values.put("mimetype", "vnd.android.cursor.item/photo");
if (photoRow >= 0) {
    cr.update(u, values, " _id= " + photoRow, null);
} else {
    cr.insert(u, values);
}
if (!Sync) {
    u = Uri.withAppendedPath(Uri.parse("content://com.android.contacts/raw_contacts"),
            String.valueOf(personId));
    values = new ContentValues();
    values.put("dirty", 0);
    cr.update(u, values, null, null);
}
设置联系人的头像:
ContentValues values = new ContentValues();
Uri u = Uri.parse("content://com.android.contacts/data");
int photoRow = -1;
String where = "raw_contact_id = " + personId
        + " AND mimetype ='vnd.android.cursor.item/photo'";
Cursor cursor = cr.query(u, null, where, null, null);
if (cursor != null) {
    if (cursor.moveToFirst()) {
        photoRow = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
    }
    cursor.close();
}
values.put("raw_contact_id", personId);
values.put("is_super_primary", 1);
values.put("data15", bytes);
values.put("mimetype", "vnd.android.cursor.item/photo");
if (photoRow >= 0) {
    cr.update(u, values, " _id= " + photoRow, null);
} else {
    cr.insert(u, values);
}
if (!Sync) {
    u = Uri.withAppendedPath(Uri.parse("content://com.android.contacts/raw_contacts"),
            String.valueOf(personId));
    values = new ContentValues();
    values.put("dirty", 0);
    cr.update(u, values, null, null);
}


强制显示overflow

try {
    ViewConfiguration mconfig = ViewConfiguration.get(this);
    Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
    if(menuKeyField != null) {
        menuKeyField.setAccessible(true);
        menuKeyField.setBoolean(mconfig, false);
    }
} catch (Exception e) {
    e.printStackTrace();
}


反射获取mITelephony接口的关键代码:

try {
    // 反射获得系统服务的getService方法对象
    Method method = Class.forName("android.os.ServiceManager")
            .getMethod("getService", String.class);
    // 执行这个方法得到一个IBinder对象
    IBinder binder = (IBinder) method.invoke(null, new Object[] {
            TELEPHONY_SERVICE
    });
    // 转换为具体的服务类(ITelephony)接口对象
    mITelephony = ITelephony.Stub.asInterface(binder);

    // 从上是通过反射来做的, 下面正常的做法>> 按下面来做,需导入android-sdk-linux/platforms/android-xx/data/layoutlib.jar
    // IBinder binder = ServiceManager.getService(TELEPHONY_SERVICE);
    // ITelephony telephony2 = ITelephony.Stub.asInterface(binder);
} catch (Exception e) {
    e.printStackTrace();
}
如果导入layoutlib.jar,编译出现类似的DEX Error错误:

[2014-09-03 11:02:45 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/android/internal/telephony/ITelephony;
[2014-09-03 11:02:45 - CareLauncher] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/android/internal/telephony/ITelephony;

应该采用用户库的形式导入该库,具体步骤如下:

1. Right-click the project in Eclipse and select "Build Path -> Add Libraries...".
2. Select User Library from the list and click Next.
3. Click the "User Libraries..." button.
4. Click "New..." in the User Libraries dialog.
5. Give the user library a name and select the System library checkbox and click OK.
6. Highlight the newly added user library in the list and click the "Add JARs..." button and add the desired jar files.
7. Click OK on the User Libraries dialog.
8. Make sure the new user library is checked in the Add Library dialog and


更新USIM、SIM卡联系人

ContentValues values = new ContentValues();
values.put("index", index);//当前联系人在SIM、USIM卡中的id
values.put("newTag", value.get(2));//新名字
values.put("newNumber", value.get(3));//新号码
if (simType.equals(ACCOUNT_TYPE_SIM)) {//判断是否为SIM卡
    values.put("tag", value.get(0));
    values.put("number", value.get(1));
    cr.update(SIMURI, values, null, null);
} else if (simType.equals(ACCOUNT_TYPE_USIM)) {//判断是否为USIM卡,USIM卡可存两个号码
    String newAnr;
    if (value.size() > 4) {
        newAnr = value.get(4);
    } else {
        newAnr = "";
    }
    values.put("newAnr", newAnr);
    cr.update(USIMURI, values, null, null);
}

删除USIM、SIM卡联系人
int count = -1;
String where = "index=" + index + "";//where必须这样子书写,不可修改
if (simType.equals(ACCOUNT_TYPE_SIM)) {
    count = cr.delete(SIMURI, where, null);
} else if (simType.equals(ACCOUNT_TYPE_USIM)) {
    count = cr.delete(USIMURI, where, null);
}
Log.v(TAG, "deleteSimOrUsim: count = " + count);


判断一个字符串是否为数字
public static boolean isNumeric(String str) {
    for (int i = str.length(); --i >= 0;) {
        if (!Character.isDigit(str.charAt(i))) {
            return false;
        }
    }
    return true;
}

将Bitmap转换程byte
public static byte[] Bitmap2Bytes(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

根据手机号获得号码所在地(MTK平台测试通过):

private String getLocation(Context context, String number) {
    GeoCodingQuery geoCodingQuery = GeoCodingQuery.getInstance(context);
    String cityName = geoCodingQuery.queryByNumber(number);
    Log.v(TAG, "[GeoCodingQuery] cityName = " + cityName);
    if ((cityName != null) && (!cityName.equals(""))) {
        return cityName;
    }
    return null;
}

ScaleGestureDetector //缩放手势



    /**
     * 从SD卡查找并保存所有lrc文件的路径和文件名保存在mLrcMap
     * 
     * @param file
     */
    private void getAllLrcPath(File file) {
        File[] files = file.listFiles(); // 将file文件数组传给files

        if (files != null) { // 判断是否存在,不判断会出异常
            for (int i = 0; i < files.length; i++) { // 逐个扫描
                if (files[i].isFile()) { // 判断如果是个文件
                    if (files[i].getName().endsWith(".lrc")) {
                        String fileName = mLrcParse.parseLine(files[i]);
                        if (TextUtils.isEmpty(fileName)) {
                            fileName = files[i].getName();
                            int pos = fileName.lastIndexOf('.');
                            fileName = fileName.substring(0, pos);
                        }
                        mLrcMap.put(fileName.toLowerCase(), files[i].getAbsolutePath());
                        Log.d(TAG, "file:" + fileName + "-->" + files[i].getAbsolutePath());
                    }
                } else {
                    getAllLrcPath(files[i]); // 递归,重新调用这个函数再扫描文件夹里面的
                }
            }
        }
    }


     /*
     * 从SD卡查找并保存所有lrc文件的路径和文件名保存在mLrcMap
     *
     * @param file
     */
    private void getAllLrcPath(File file) {
        File[] files = file.listFiles(); // 将file文件数组传给files

        if (files != null) { // 判断是否存在,不判断会出异常
            for (int i = 0; i < files.length; i++) { // 逐个扫描
                if (files[i].isFile()) { // 判断如果是个文件
                    if (files[i].getName().endsWith(".lrc")) {
                        String fileName = mLrcParse.parseLine(files[i]);
                        if (TextUtils.isEmpty(fileName)) {
                            fileName = files[i].getName();
                            int pos = fileName.lastIndexOf('.');
                            fileName = fileName.substring(0, pos);
                        }
                        mLrcMap.put(fileName.toLowerCase(), files[i].getAbsolutePath());
                        Log.d(TAG, "file:" + fileName + "-->" + files[i].getAbsolutePath());
                    }
                } else {
                    getAllLrcPath(files[i]); // 递归,重新调用这个函数再扫描文件夹里面的
                }
            }
        }
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值