转载说明

本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/android-code-snippets/

说明

此篇文章为个人日常使用所整理的一此代码片段,此篇文正将会不定时更新

代码

评价应用

activity.startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=" + activity.getPackageName())));

获得系统分享列表

/** * 获得系统分享列表 * @param context * @return */public static List< ResolveInfo > getShareApps ( Context context ){    Intent intent = new Intent ( Intent.ACTION_SEND, null );    intent.addCategory ( Intent.CATEGORY_DEFAULT );    intent.setType ( "*/*" );    PackageManager pManager = context.getPackageManager ();    return pManager.queryIntentActivities ( intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT );//        for ( int i = 0; i < resolveInfos.size (); i++ )//        {//            AppInfoVo appInfoVo = new AppInfoVo ();//            ResolveInfo resolveInfo = resolveInfos.get ( i );//            appInfoVo.setAppName ( resolveInfo.loadLabel ( packageManager ).toString () );//            appInfoVo.setIcon ( resolveInfo.loadIcon ( packageManager ) );//            appInfoVo.setPackageName ( resolveInfo.activityInfo.packageName );//            appInfoVo.setLauncherName ( resolveInfo.activityInfo.name );//            appInfoVos.add ( appInfoVo );//        }//        return appInfoVos;}

获得当前IP地址

HttpUtils.GetHtml ( "http://1111.ip138.com/ic.asp", null, new HttpUtils.HttpUtilsCallBack (){    @Override    public void OnFinish ( HttpResponse httpResponse, int resultCode, String resultString )    {        Pattern p = Pattern.compile ( "\\[(.+)\\]" );        Matcher m = p.matcher ( resultString );        if ( m.find () )        {            String ipAddress = m.group ( 1 );            LogUtils.OutputDebugString ( ipAddress );        }    }    @Override    public void OnError ( Exception e )    {        LogUtils.OutputDebugString ( e );    }} );

获得当前Activity的根视图

/** * 获得当前Activity的根视图 * @param activity * @return */public static ViewGroup GetContentView(Activity activity){    return ( ViewGroup ) ( ( ViewGroup ) activity.findViewById ( android.R.id.content ) ).getChildAt ( 0 );}

打开应用

public static void OpenApp(Context context, String packageName_){    try    {        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);        resolveIntent.setPackage(context.getPackageManager().getPackageInfo(packageName_, 0).packageName);        List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(resolveIntent, 0);        ResolveInfo ri = apps.iterator().next();        if (ri != null)        {            String packageName = ri.activityInfo.packageName;            String className = ri.activityInfo.name;            Intent intent = new Intent(Intent.ACTION_MAIN);            intent.addCategory(Intent.CATEGORY_LAUNCHER);            ComponentName cn = new ComponentName(packageName, className);            intent.setComponent(cn);            context.startActivity(intent);        }    }    catch (PackageManager.NameNotFoundException e)    {        e.printStackTrace();    }}

打开URL

public static void OpenUrl(Context context, String url){    android.content.Intent intent = new android.content.Intent();    intent.setAction("android.intent.action.VIEW");    intent.setData(android.net.Uri.parse(url));    context.startActivity(intent);}public static void OpenUrl(Context context, int url){    android.content.Intent intent = new android.content.Intent();    intent.setAction("android.intent.action.VIEW");    intent.setData(android.net.Uri.parse(context.getString(url)));    context.startActivity(intent);}

创建桌面快捷方式

public static void CreateShortcut(Context context, String appName, Class<?> startClass, int icon){    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);    shortcut.putExtra("duplicate", false);// 设置是否重复创建    Intent intent = new Intent(Intent.ACTION_MAIN);    intent.addCategory(Intent.CATEGORY_LAUNCHER);    intent.setClass(context, startClass);// 设置第一个页面    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);    Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, icon);    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);    context.sendBroadcast(shortcut);}

分享图片到微信朋友圈

public static void shareMultiplePictureToTimeLine ( Context context, File... files ){    Intent intent = new Intent ();    ComponentName comp = new ComponentName ( "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI" );    intent.setComponent ( comp );    intent.setAction ( Intent.ACTION_SEND_MULTIPLE );    intent.setType ( "image/*" );    ArrayList< Uri > imageUris = new ArrayList< Uri > ();    for ( File f : files )    {        imageUris.add ( Uri.fromFile ( f ) );    }    intent.putParcelableArrayListExtra ( Intent.EXTRA_STREAM, imageUris );    context.startActivity ( intent );}

状态栏透明

/** * 设置状态栏透明 * @param activity */public static void TranslucentStatus(Activity activity){    if ( android.os.Build.VERSION.SDK_INT > 18 )    {        Window window = activity.getWindow ();        window.setFlags ( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS    , WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS     );        window.setFlags ( WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION );    }}

ProgressDialogLoading

public static void ProgressDialogLoading ( final Context context, final ProgressDialogLoadingCallBack progressDialogLoadCallBack ){    final ProgressDialog progressDialog = new ProgressDialog ( context );    progressDialogLoadCallBack.onInit ( context, progressDialog );    new Thread ( new Runnable ()    {        @Override        public void run ()        {            progressDialogLoadCallBack.onRun ( context, progressDialog );            progressDialog.dismiss ();        }    } ).start ();}public static interface ProgressDialogLoadingCallBack{    public void onInit ( Context context, ProgressDialog progressDialog );    public void onRun ( Context context, ProgressDialog progressDialog );}

ImageView 设置图片

ImageView.setImageResource(R.drawable.icon);

杀死对应的Android程序

/** * 杀死对应的Android程序,而不会自动启动 * @param pkgName 应用程序的包名 */public static void forceStopAPK ( String pkgName ) throws Exception{    Process sh = Runtime.getRuntime ().exec ( "su" );    DataOutputStream os = new DataOutputStream ( sh.getOutputStream () );    final String Command = "am force-stop " + pkgName + "\n";    os.writeBytes ( Command );    os.flush ();    sh.waitFor ();}

备注

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 【Android】Toast大全
  3. [置顶] Eclipse,到了说再见的时候了——Android(安卓)Studio最全
  4. http上传文件到网络核心代码
  5. fanfou(饭否) android客户端 代码学习2
  6. android调用本地录制程序获取录制文件路径的问题(摘)
  7. Android开发实践 BroadcastReceiver
  8. android 获取程序的安装时间
  9. Android变形(Transform)之Camera使用介绍

随机推荐

  1. Android之uri、file、path相互转化
  2. Android(安卓)Gradle Build Error:Some f
  3. Android(安卓)GPS NETWORK定位
  4. android 自定义线程,自动结束本身线程
  5. android FileNotFoundException错误:创建
  6. android实现状态栏添加图标的函数实例
  7. 安卓APP:利用AndroidStudio开发usb串口通
  8. 【Android】导入第三方库时,关于so文件的
  9. Android7.0调用系统相机和裁剪
  10. Android(安卓)Intent Action 大全