您的位置:首页 > 移动开发 > Android开发

Android小知识汇总

2011-09-02 09:44 337 查看
1、自定义应用标题

在Activity的onCreate方法中加入以下代码,顺序要注意,不然发现效果不对。

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.mainactivity);    //Activity的布局
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,布局资源id);   //标题栏的布局


2、关闭屏幕旋转。在AndroidManifest.xml注册Activity时加上后面两个属性。android:screenOrientation:屏幕方向。

<activity android:name=".FlowActivity" android:configChanges="keyboardHidden|orientation"
android:screenOrientation="landscape" />


3、卸载应用。

private void uninstallPkg(String packageName) {
// Create new intent to launch Uninstaller activity
Uri packageURI = Uri.parse("package:"+packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
setIntentAndFinish(true, true);
}


4、获得文件夹大小及可用空间,例:应用安装区大小。

String path = Environment.getDataDirectory().getPath();
StatFs stat = new StatFs(path);
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long availableBlocks = stat.getAvailableBlocks();


5、关闭/恢复窗口动画,需要系统权限。

//关闭窗口动画
IWindowManager mWindowManager = IWindowManager.Stub.asInterface(ServiceManager
.getService("window"));
float[] mAnimationScales;
try {
mAnimationScales = mWindowManager.getAnimationScales();//保存关闭之前窗口动画效果
mWindowManager.setAnimationScales(new float[] { 0, 0 });
} catch (RemoteException e) {
e.printStackTrace();
}
//恢复窗口动画
mWindowManager.setAnimationScales(mAnimationScales);


6、设置中如何跳转到应用详细信息界面。

Intent intent = new Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", 应用包名 , null));
// start new activity to display extended information
startActivityForResult(intent, 1);


7、屏幕旋转时不调用onCreate方法。

manifest.xml中注册activity时加入属性:android:configChanges="orientation|keyboardHidden"

Activity中重写onConfigurationChanged方法:

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}


8、安装应用

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://xxx.apk","application/vnd.android.package-archive");
startActivity(intent);


9、从应用进入主界面(Launcher)

Intent i = new Intent(Intent.ACTION_MAIN);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //如果是服务里调用,必须加入new task标识
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);


10、灭屏

try {
IPowerManager power = IPowerManager.Stub.asInterface(
ServiceManager.getService("power"));
if (power != null) {
power.goToSleep(SystemClock.uptimeMillis());
}
} catch (RemoteException doe) {
}


需要权限<uses-permission android:name="android.permission.DEVICE_POWER"/>

11、亮屏

PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP,"");
wakeLock.acquire();

需要权限<uses-permission android:name="android.permission.WAKE_LOCK"/>

PARTIAL_WAKE_LOCK :保持CPU 运转,屏幕和键盘灯有可能是关闭的。

SCREEN_DIM_WAKE_LOCK :保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯

SCREEN_BRIGHT_WAKE_LOCK :保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯
FULL_WAKE_LOCK :保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度

12、获取正在运行的程序所占内存大小(从设置中扒出来的代码)

Debug.MemoryInfo[] mems;
try {
mems = ActivityManagerNative.getDefault()
.getProcessMemoryInfo(pids_array);
int i = 0;
for(Debug.MemoryInfo mem : mems){
double mSize = ((double)mem.getTotalPss()) * 1024;
//System.out.println(pids_array[i] + " - " + mSize);
}
} catch (RemoteException e) {
e.printStackTrace();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: