最近又折腾上安卓了,开发了两个APP,相比于iOS,android的开发还是感觉更容易一点。界面layout xml非常强大。
我整理一些网上比较难找到,或者自己琢磨出来的一些经验在这个文章里列出来,不定期更新(可能就不更新了。。。)
//返回歌曲id列表
public List<Integer> querySongs(Context context) {
ArrayList<Integer> list = null;
String[] columns = { MediaStore.Audio.AudioColumns._ID
,MediaStore.Audio.AudioColumns.IS_MUSIC};
Cursor c = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns, null,
null, null);
list = new ArrayList<Integer>();
while (c.moveToNext())
{
if(c.getInt(1) != 0) {
list.add(c.getInt(0));
}
}
return list;
}
public Bundle querySong(Context context,int id) {
Bundle bundle = null;
Uri uri = Uri.withAppendedPath( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
Integer.toString(id) );
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{
MediaStore.Audio.AudioColumns.ARTIST
,MediaStore.Audio.AudioColumns.TITLE}, null, null, null);
if(cursor == null) {
//权限不允许
return null;
}
if (cursor.moveToFirst()) {
bundle = new Bundle();
bundle.putInt("id", id);//歌曲id
bundle.putString("artist", cursor.getString(0));//作者
bundle.putString("title", cursor.getString(1));//歌曲标题
}
cursor.close();
return bundle;
}
//mediaId是歌曲id
Uri uri = Uri.parse("content://media/external/audio/media/" + mediaId + "/albumart");
imageView.setImageURI(uri);
public Bundle queryContact(Context context,String number) {
Bundle bundle = null;
Uri uri = Uri.parse(ContactsContract.PhoneLookup.CONTENT_FILTER_URI + "/" + number);
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{PhoneLookup._ID,PhoneLookup.DISPLAY_NAME}, null, null, null);
if(cursor == null) {
//权限不允许
return null;
}
if (cursor.moveToFirst()) {
long id = cursor.getLong(0);
String name = cursor.getString(1);
bundle = new Bundle();
bundle.putLong("id", id);//id
bundle.putString("name", name);//名字
Uri uri1 = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(context.getContentResolver(), uri1);
Bitmap bitmap = BitmapFactory.decodeStream(input);
bundle.putParcelable("avatar", bitmap);//头像
}
cursor.close();
return bundle;
}
public static long getToday() {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTimeInMillis();
}
void expandNotify(Context context) {
int currentApiVersion = android.os.Build.VERSION.SDK_INT;
try {
Object service = context.getSystemService("statusbar");
Class<?> statusbarManager = Class
.forName("android.app.StatusBarManager");
Method expand = null;
if (service != null && statusbarManager != null) {
if (currentApiVersion <= 16) {
expand = statusbarManager.getMethod("expand");
expand.setAccessible(true);
expand.invoke(service);
} else {
expand = statusbarManager
.getMethod("expandNotificationsPanel");
}
expand.setAccessible(true);
expand.invoke(service);
}
} catch (Exception e) {
}
}
String readFromAssets(Context context,String fileName){
try {
InputStreamReader inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
void brightScreen(Context context) {
//获取电源管理器对象
PowerManager powerMgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
PowerManager.WakeLock wl = powerMgr.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
//点亮屏幕
wl.acquire();
wl.release();
}