读书人

个人android札记(二)

发布时间: 2012-07-15 20:11:40 作者: rapoo

个人android笔记(二)
1、卸载某一应用,首先应该知道该应用的包名

public static void uninstallApk(Context context, String packageName) {Uri packageURI = Uri.parse("package:" + packageName);Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);context.startActivity(uninstallIntent);}


2、要设置某个控件的text,使用textview.setText(R.String.*)来实现是非常规甚至是不妥的,正确的方式是使用Context.getString(R.String.*),如下:
CharSequence text = getString(R.string.app_name);    textview.setText(text);

另外在value/Strings.xml中定义特殊字符时,如"?""\"需要使用转义字符\?,\\

3、代码片段:获得手机分辨率:
 public String getResolution(){//获得屏幕分辨率        DisplayMetrics dm = new DisplayMetrics();       getWindowManager().getDefaultDisplay().getMetrics(dm);       return dm.widthPixels + "*" + dm.heightPixels;  }


4、样式的使用
(1)首先在res/values/style.xml中定义
<?xml version="1.0" encoding="utf-8"?><resources>    <style name="mystyle">    <item name="android:textSize">18sp</item>    <item name="android:textColor">#EC9237</item>    <item name="android:fromAlpha">0.0</item>    <item name="android:toAlpha">0.0</item>    </style></resources>

(2)在TextView中:
<TextView      style="@style/mystyle"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="hello world!!"    />

5、代码片段:新应用第一次启动,创建快捷方式
(1)判断是否是第一次启动:
 private void doAtFirstRun() {     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);     SharedPreferences.Editor editor = settings.edit();           if (!settings.getBoolean(FIRST_RUN_TAG, false)) {//只有第一次安装才创建icon            editor.putBoolean(FIRST_RUN_TAG, true);            createShortcut();                    }            editor.commit();         }

(2)创建快捷方式
private void createShortcut() {            Intent shortcutIntent = null;        shortcutIntent = new Intent(Intent.ACTION_MAIN);        shortcutIntent.setClassName(this, this.getClass().getName());        shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);        final Intent intent = new Intent();        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon));                intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");        sendBroadcast(intent);           }

(3)别忘记了加权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

读书人网 >Android

热点推荐