转载说明

本篇文章可能已经更新,最新文章请转: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 ();
}

备注

Android - 代码片段的更多相关文章

  1. 【Android代码片段之六】Toast工具类(实现带图片的Toast消息提示)

    转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/6841266  作者:张燕广 实现的Toast工具类ToastUtil封装 ...

  2. 【Android代码片段之八】监听Android屏幕是否锁屏

    实现方法:1)通过BroadcastReceiver接收广播Intent.ACTION_SCREEN_ON和Intent.ACTION_SCREEN_OFF可以判断屏幕状态是否锁屏,但是只有屏幕状态发 ...

  3. android代码片段二

      1.Android拦截短信 一.AndroidManifest.xml <uses-permission android:name="android.permission.RECE ...

  4. 实用的Android代码片段集合(精)

    1.精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) public static double getScreenPhysicalSize(Activity ctx) { DisplayMetr ...

  5. 给大家介绍几个常见的Android代码片段

    今天在源码天堂那个网站,也下载了一个不错的Android源码特效,现在分享一下给博客园的朋友吧,个人觉得那个网站还是挺不错的,希望大家能够使用得上. 仿美图秀秀拼图功能源码 仿美图秀秀拼图功能源码,最 ...

  6. android代码片段一

    1.Android判断是Pad或者手机 public boolean isTabletDevice() { TelephonyManager telephony = (TelephonyManager ...

  7. Android课程---Android Studio使用小技巧:提取方法代码片段

    这篇文章主要介绍了Android Studio使用小技巧:提取方法代码片段,本文分享了一个快速复制粘贴方法代码片段的小技巧,并用GIF图演示,需要的朋友可以参考下 今天来给大家介绍一个非常有用的Stu ...

  8. Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)

    摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...

  9. 转--Android实用的代码片段 常用代码总结

    这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下     1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...

随机推荐

  1. 通过程序 VB.Net 或 C# 读取文本文件行数

    1, VB.NET 读取 (通过streamReader) ' tmpCount = 0 'Dim tmpSR As New StreamReader(fileFullName, System.Tex ...

  2. 杭电 3177 Crixalis&#39;s Equipment

    http://acm.hdu.edu.cn/showproblem.php? pid=3177 Crixalis's Equipment Time Limit: 2000/1000 MS (Java/ ...

  3. Web之真假分页

    在web设计中一个无法避免的问题就是分页显示.当数据量特别大的时候,我们不可能将全部的数据都在一个页面进行显示,假设这样将严重影响到它的美观性.所以在这个时候,分页显示则成为了我们的大功臣.当然分页也 ...

  4. DashClock

    https://github.com/romannurik/dashclock/ https://github.com/nhaarman/DashPinkpop dashclock-master.zi ...

  5. onClick,onServerClick,onClientClick

    <asp:button id=button1 runat=server test=button1 onclick=button1_onclick/> <input type=butt ...

  6. 解读Unity中的CG编写Shader系列4——unity中的圆角矩形shader

    上篇文章中我们掌握了表面剔除和剪裁模式 这篇文章将利用这些知识实现一个简单的,可是又非经常常使用的样例:把一张图片做成圆角矩形 例3:圆角矩形Shader 好吧我承认在做这个样例的时候走了不少弯路,因 ...

  7. qsort函数、sort函数 (精心整理篇)

    先说明一下qsort和sort,只能对连续内存的数据进行排序,像链表这样的结构是无法排序的. 首先说一下, qsort qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有 ...

  8. 循环语句until和while

    一.until语句的基本格式 until 条件测试 do 语句块 done 只要条件测试语句未成功结束,则执行语句块.(如果一开始条件测试语句就成功退出,那么一次也不执行语句块.这里跟C语言中的do. ...

  9. 【分布式计算】MapReduce的替代者-Parameter Server

    原文:http://blog.csdn.net/buptgshengod/article/details/46819051 首先还是要声明一下,这个文章是我在入职阿里云1个月以来,对于分布式计算的一点 ...

  10. java_数组作缓存池的不可变类实例

    package ming; public class CacheImmutale { private static int MAX_SIZE = 10; private static CacheImm ...