1.扫描全盘,刷新音乐视频列表:
(1)<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
(2)
/**
* 扫描全盘
**/
public void allScan(){
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}
2.获取最近使用的4个应用:
(1) <uses-permission android:name="android.permission.GET_TASKS"/>
(2)
public class RecentAppActivity extends Activity {
final ImageView[] mIcons = new ImageView[4];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIcons[0]=(ImageView)findViewById(R.id.iv_01);
mIcons[1]=(ImageView)findViewById(R.id.iv_02);
mIcons[2]=(ImageView)findViewById(R.id.iv_03);
mIcons[3]=(ImageView)findViewById(R.id.iv_04);
getRecentApp();
}
public void getRecentApp() {
final PackageManager pm = this.getPackageManager();
final ActivityManager am = (ActivityManager)
this.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RecentTaskInfo> recentTasks =
am.getRecentTasks(6, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
Log.e("listnum:",""+recentTasks.size());
for(int i=2;i<recentTasks.size();i++){
final ActivityManager.RecentTaskInfo info = recentTasks.get(i);
Intent intent = new Intent(info.baseIntent);
ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
if(resolveInfo!=null){
final ActivityInfo activityInfo = resolveInfo.activityInfo;
final String title = activityInfo.loadLabel(pm).toString();
Log.e("rencentapp name:", title);
mIcons[i-2].setImageDrawable(resolveInfo.loadIcon(pm));
}
mIcons[i-2].setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
Intent intent=new Intent(info.baseIntent);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RecentAppActivity.this.startActivity(intent);
}
});
}
}
}
3.获取所有应用:
/**
* 获取所有机器上所有APK
* @return APK数量
*/
public int getAllApks() {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
packageManager = this.getPackageManager();
apps = packageManager.queryIntentActivities(mainIntent, 0);
return apps.size();
}
4.通过包名和类名获取应用图标和名字:
public void getData(){
imgs.clear();
names.clear();
for(int i=0;i<cur.getCount();i++){
cur.moveToPosition(i);
String pkg=cur.getString(cur.getColumnIndex("_pkg"));
String cls=cur.getString(cur.getColumnIndex("_cls"));
ComponentName componentName = new ComponentName(pkg, cls);
PackageManager pkm=data.getBaseContext().getPackageManager();
try {
Drawable img=pkm.getActivityInfo(componentName, ActivityInfo.FLAG_STATE_NOT_NEEDED).loadIcon(pkm);
imgs.add(img);
String label=pkm.getActivityInfo(componentName, ActivityInfo.FLAG_STATE_NOT_NEEDED).loadLabel(pkm)+"";
names.add(label);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.列表item中checkbox,注意:由于checkbox的点击事件优先级比listview的高,所以要添加android:focusable="false"属性,使得checkbox初始的时候没有获取焦点。另外这里是点击ListView的item控制checkbox的状态改变,也就是让item接收clik事件,所以需要加android:focusableInTouchMode="false"这一属性。
<CheckBox
android:id="@+id/box_lv"
android:layout_width="30px"
android:layout_height="30px"
android:visibility="invisible"
android:clickable="false"
android:focusableInTouchMode="false"
android:focusable="false"
android:layout_gravity="center_vertical"/>
6.针对不同分辨率写不同layout文件
(1)<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
(2)