前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android Development Code Snippets

Android Development Code Snippets

作者头像
宅男潇涧
发布2018-08-01 15:39:34
3590
发布2018-08-01 15:39:34
举报
文章被收录于专栏:潇涧技术专栏潇涧技术专栏

Android开发代码片段

1.获取应用的版本信息

通过PackageInfo类来得到版本信息

代码语言:javascript
复制
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String versionName = pInfo.versionName;
int versionCode = pInfo.versionCode;

2.进入应用市场给应用评分

构造应用程序在应用市场中对应的网址,打开即可

代码语言:javascript
复制
Uri uri = Uri.parse("market://details?id=" + getPackageName());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

3.安装/卸载应用

打开查看某个APK文件就是安装应用操作

代码语言:javascript
复制
File apkfile = "/path/to/your/apk/file";
if (!apkfile.exists()) {
    return;
}

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
startActivity(intent);

卸载应用

代码语言:javascript
复制
private void uninstall(String packageName) {
    Uri uri = Uri.parse("package:" + packageName);
    Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, uri);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

4.在屏幕的中间显示Toast信息

设置setGravity即可

代码语言:javascript
复制
private static void showMiddleToast(Context context, String msg) {
    Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();
}

5.复制文本到剪贴板

使用ClipboardManager

代码语言:javascript
复制
public static void copy(Context context, String content) {
    ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboardManager.setText(content);
}

6.用户按两次返回键退出应用

记录上一次按返回键的时间exitTime,并给定一个时间范围(2 秒钟)

代码语言:javascript
复制
public void onBackPressed() {
    exitApp();
}

private long exitTime = 0;

private void exitApp() {
    if ((System.currentTimeMillis() - exitTime) > 2000) {
        showButtomToast("再按一次退出");
        exitTime = System.currentTimeMillis();
    } else {
        finish();
    }
}

7.创建桌面快捷方式

创建桌面快捷方式实际上就是构造一个Intent然后将其广播出去

代码语言:javascript
复制
@Click
void shortcut() {
    new AsyncTask<String, Void, Boolean>() {
        @Override
        protected void onPreExecute() {
            Toast.makeText(ShortcutActivity.this, "正在创建快捷方式...", Toast.LENGTH_SHORT).show();
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            try {
                Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
                shortcutIntent.putExtra("duplicate", false);//不重复创建
                shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, params[0]);

                Parcelable icon = Intent.ShortcutIconResource.fromContext(ShortcutActivity.this, R.drawable.polaris);
                shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

                Intent intent = new Intent(Intent.ACTION_MAIN);
                //指定class的名称是为了在卸载应用程序时,同时删除桌面快捷键的图标
                ComponentName componentName = new ComponentName(ShortcutActivity.this, ShortcutActivity.class);
                intent.setComponent(componentName);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                intent.addCategory(Intent.CATEGORY_DEFAULT);

                shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
                ShortcutActivity.this.sendBroadcast(shortcutIntent);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (null != result && result) {
                Toast.makeText(ShortcutActivity.this, "创建快捷方式成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(ShortcutActivity.this, "创建快捷方式失败", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(result);
        }
    }.execute("Polaris");
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015/5/31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档